InventoryDAL.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.DataLogic.Repositories;
  6. using EMIS.ViewModel.TeachingMaterial;
  7. using System.Linq.Expressions;
  8. using EMIS.Entities;
  9. using EMIS.ViewModel;
  10. using EMIS.ViewModel.Cultureplan;
  11. using EMIS.DataLogic.Common.Cultureplan;
  12. namespace EMIS.DataLogic.Common.TeachingMaterial
  13. {
  14. //库存
  15. public class InventoryDAL
  16. {
  17. #region --0.0 定义--
  18. public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; }
  19. public TeachingMateriaInventoryRepository TeachingMateriaInventoryRepository { get; set; }
  20. public UserRepository UserRepository { get; set; }
  21. public CoursematerialRepository CoursematerialRepository { get; set; }
  22. public DictionaryItemRepository DictionaryItemRepository { get; set; }
  23. public PublishRepository PublishRepository { get; set; }
  24. public InventoryCollectRepository InventoryCollectRepository { get; set; }
  25. public SchoolyearRepository SchoolyearRepository { get; set; }
  26. public StockOutRepository StockOutRepository { get; set; }
  27. public StockOutDetailRepository StockOutDetailRepository { get; set; }
  28. public StockInRepository StockInRepository { get; set; }
  29. public StockInDetailRepository StockInDetailRepository { get; set; }
  30. public StudentDistributeRepository StudentDistributeRepository { get; set; }
  31. public Lazy<CoursematerialDAL> CoursematerialDAL { get; set; }
  32. #endregion
  33. #region 1.0 获取教材库存信息
  34. /// <summary>
  35. /// 获取教材库存信息列表
  36. /// </summary>
  37. /// <param name="exp"></param>
  38. /// <returns></returns>
  39. public IQueryable<InventoryView> GetTeachingMaterialInventoryGridView(Expression<Func<ET_TeachingMateriaInventory, bool>> exp, Expression<Func<CF_TeachingMaterialPool, bool>> poolexp)
  40. {
  41. var view = (from l in TeachingMaterialPoolRepository.GetList(poolexp)
  42. join t in TeachingMateriaInventoryRepository.GetList(exp) on l.TeachingMaterialPoolID equals t.TeachingMaterialPoolID into tsct
  43. from tm in tsct.DefaultIfEmpty()
  44. //join d in DictionaryItemRepository.Entities
  45. //on new { l.TeachingMaterialTypeID, DictionaryCode = typeof(CF_TeachingMaterialType).Name }
  46. //equals new { TeachingMaterialTypeID = d.Value, d.DictionaryCode } into dd
  47. //from ed in dd.DefaultIfEmpty()
  48. join u in UserRepository.Entities on l.CreateUserID equals u.UserID
  49. into uTmp
  50. from UserTabel in uTmp.DefaultIfEmpty()
  51. join p in PublishRepository.Entities on l.PublishID equals p.PublishID
  52. into pp
  53. from ppp in pp.DefaultIfEmpty()//改为左联
  54. group new
  55. {
  56. TeachingMaterial = l,
  57. Inventory = tm,
  58. //TeachingMaterialType = ed,
  59. User = UserTabel,
  60. Publish = ppp
  61. } by new
  62. {
  63. Author = l.Author,
  64. ISBN = l.ISBN,
  65. Price = l.Price,
  66. TeachingMaterialCode = l.TeachingMaterialCode,
  67. TeachingMaterialName = l.TeachingMaterialName,
  68. TeachingMaterialShorName = l.TeachingMaterialShorName,
  69. TeachingMaterialPoolID = l.TeachingMaterialPoolID,
  70. TeachingMaterialTypeID = l.TeachingMaterialTypeID,
  71. //TeachingMaterialTypeName = ed.Name,
  72. PublishID = l.PublishID,
  73. PublishName = ppp.UnitName,
  74. PublishTime = l.PublishTime,
  75. CreateTime = l.CreateTime,
  76. CreateUserName = UserTabel.Name,
  77. Desc = l.Desc,
  78. IsLate = l.IsLate.Value,
  79. IsLateName = l.IsLate == true ? "是" : "否",
  80. MinInventory = l.MinInventory == null ? 1 : l.MinInventory,
  81. //ModifyTime = tm.ModifyTime == null ? l.ModifyTime : tm.ModifyTime
  82. } into g
  83. join tm in TeachingMaterialPoolRepository.Entities on g.Key.TeachingMaterialPoolID equals tm.TeachingMaterialPoolID
  84. select new InventoryView
  85. {
  86. Author = g.Key.Author,
  87. ISBN = g.Key.ISBN,
  88. Price = g.Key.Price,
  89. TeachingMaterialCode = g.Key.TeachingMaterialCode,
  90. TeachingMaterialName = g.Key.TeachingMaterialName,
  91. TeachingMaterialShorName = g.Key.TeachingMaterialShorName,
  92. TeachingMaterialPoolID = g.Key.TeachingMaterialPoolID,
  93. TeachingMaterialTypeID = g.Key.TeachingMaterialTypeID,
  94. //TeachingMaterialTypeName = g.Key.TeachingMaterialTypeName,
  95. PublishID = g.Key.PublishID,
  96. PublishName = g.Key.PublishName,
  97. PublishTime = g.Key.PublishTime,
  98. CreateTime = g.Key.CreateTime,
  99. CreateUserName = g.Key.CreateUserName,
  100. Desc = g.Key.Desc,
  101. IsLate = g.Key.IsLate,
  102. IsLateName = g.Key.IsLateName,
  103. OrderQty = 1,
  104. CoursematerialEntityList = tm.EM_Coursematerial,
  105. MinInventory = g.Key.MinInventory,
  106. PresentInventory = g.Sum(x => x.Inventory.PresentInventory ?? 0),
  107. Total = g.Sum(x => x.Inventory.PresentInventory ?? 0) * g.Key.Price,//(码洋=单价*当前库存量)
  108. ModifyTime = g.Max(x => x.Inventory.ModifyTime),
  109. });
  110. return view;
  111. }
  112. public IQueryable<CoursematerialView> GetTeachingMaterialInventoryCoursematerialView(Expression<Func<ET_TeachingMateriaInventory, bool>> exp)
  113. {
  114. var query = (from l in TeachingMaterialPoolRepository.Entities
  115. from lc in l.EM_Coursematerial
  116. join c in CoursematerialDAL.Value.GetCoursematerialViewQueryable(x => true)
  117. on lc.CoursematerialID equals c.CoursematerialID
  118. select c
  119. );
  120. return query;
  121. }
  122. #endregion
  123. public IQueryable<InventoryView> GetInventoryManagementGridOnlyNumView(Guid? schoolyearID)
  124. {
  125. int? yearValue = 0;
  126. string YearCode = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Code;
  127. if (schoolyearID != null)
  128. {
  129. yearValue = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Value;
  130. }
  131. #region 0.0 找出当前学年的出入库数量
  132. var oldStockIns = from si in StockInRepository.GetList(x => x.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  133. join sid in StockInDetailRepository.Entities on si.StockInID equals sid.StockInID
  134. group sid by sid.TeachingMaterialPoolID
  135. into gsid
  136. select new
  137. {
  138. TeachingMaterialPoolID = (Guid)gsid.Key,
  139. InQty = (int?)gsid.Sum(x => x.Quantity),
  140. OutQty = (int?)0,
  141. };
  142. var oldStockOuts = from a in StockOutRepository.Entities.Where(x => x.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  143. join b in StockOutDetailRepository.Entities on a.StockOutID equals b.StockOutID
  144. group b by b.TeachingMaterialPoolID
  145. into stockout
  146. select new
  147. {
  148. TeachingMaterialPoolID = stockout.Key,
  149. InQty = (int?)0,
  150. OutQty = (int?)stockout.Sum(x => x.StockOutQuantity),
  151. };
  152. var oldStockInQry = from b in oldStockIns
  153. join c in oldStockOuts on b.TeachingMaterialPoolID equals c.TeachingMaterialPoolID
  154. into cc
  155. from ccc in cc.DefaultIfEmpty()
  156. select new
  157. {
  158. TeachingMaterialPoolID = (Guid)b.TeachingMaterialPoolID,
  159. oldInQty = b.InQty,
  160. oldOutQty = ccc.OutQty ?? 0,
  161. };
  162. var oldInAndOut = from b in oldStockIns.Concat(oldStockOuts)
  163. group b by b.TeachingMaterialPoolID
  164. into aa
  165. select new
  166. {
  167. TeachingMaterialPoolID = (Guid)aa.Key,
  168. oldInQty = aa.Sum(x => x.InQty),
  169. oldOutQty = aa.Sum(x => x.OutQty),
  170. };
  171. #endregion
  172. #region 0.1 找出当前学年之后的出入库数量汇总(包含当前学年) 列: >=2015-201601 之后
  173. #region 1.0 入库
  174. var StockInview = from a in StockInRepository.Entities.Where(x => x.CF_Schoolyear.Value >= yearValue && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  175. join b in StockInDetailRepository.Entities on a.StockInID equals b.StockInID
  176. group b by b.TeachingMaterialPoolID
  177. into stockin
  178. select new
  179. {
  180. TeachingMaterialPoolID = (Guid)stockin.Key,
  181. InQty = stockin.Sum(x => x.Quantity),
  182. OutQty = (int?)0
  183. };
  184. #endregion
  185. #region 2.0 出库
  186. var StockOutview = from a in StockOutRepository.Entities.Where(x => x.CF_Schoolyear.Value >= yearValue && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  187. join b in StockOutDetailRepository.Entities on a.StockOutID equals b.StockOutID
  188. group b by b.TeachingMaterialPoolID
  189. into stockout
  190. select new
  191. {
  192. TeachingMaterialPoolID = stockout.Key,
  193. InQty = (int?)0,
  194. OutQty = stockout.Sum(x => x.StockOutQuantity),
  195. };
  196. #endregion
  197. #region 3.0 合并出入库数据进行分组
  198. var InByOutStockInfoGroup = from b in oldStockIns
  199. join c in oldStockOuts on b.TeachingMaterialPoolID equals c.TeachingMaterialPoolID
  200. into cc
  201. from ccc in cc.DefaultIfEmpty()
  202. select new
  203. {
  204. TeachingMaterialPoolID = (Guid)b.TeachingMaterialPoolID,
  205. InQty = b.InQty,
  206. OutQty = ccc.OutQty ?? 0,
  207. };
  208. var InAndOut = from b in StockInview.Concat(StockOutview)
  209. group b by b.TeachingMaterialPoolID
  210. into aa
  211. select new
  212. {
  213. TeachingMaterialPoolID = (Guid)aa.Key,
  214. // SchoolyearID = h.Key.SchoolyearID,
  215. InQty = aa.Sum(x => x.InQty),
  216. OutQty = aa.Sum(x => x.OutQty),
  217. };
  218. #endregion
  219. #endregion
  220. #region 4.0 对分组后的出入库数据跟教材库存做统计输出~
  221. var InventoryViews = from a in TeachingMaterialPoolRepository.Entities.Where(x => x.IsLate == false)
  222. join t in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals t.TeachingMaterialPoolID
  223. group t by t.TeachingMaterialPoolID
  224. into tea
  225. join InByOut in InAndOut on tea.Key equals InByOut.TeachingMaterialPoolID
  226. into InByOutdata
  227. from InByOuttb in InByOutdata.DefaultIfEmpty()
  228. join oldstin in oldInAndOut on tea.Key equals oldstin.TeachingMaterialPoolID
  229. into toldstin
  230. from ctoldstin in toldstin.DefaultIfEmpty()
  231. join p in PublishRepository.Entities on tea.FirstOrDefault().CF_TeachingMaterialPool.PublishID equals p.PublishID
  232. into pp
  233. from ppp in pp.DefaultIfEmpty()
  234. select new InventoryView
  235. {
  236. SchoolYearName = YearCode,
  237. TeachingMaterialPoolID = tea.Key,
  238. // SchoolYearID = InByOuttb.SchoolyearID == null ? (Guid)schoolyearID : InByOuttb.SchoolyearID,
  239. PublishID = ppp.PublishID,
  240. TeachingMaterialTypeID = tea.FirstOrDefault().CF_TeachingMaterialPool.TeachingMaterialTypeID,
  241. TeachingMaterialCode = tea.FirstOrDefault().CF_TeachingMaterialPool.TeachingMaterialCode,
  242. TeachingMaterialName = tea.FirstOrDefault().CF_TeachingMaterialPool.TeachingMaterialName,
  243. CoursematerialEntityList = tea.FirstOrDefault().CF_TeachingMaterialPool.EM_Coursematerial,
  244. ISBN = tea.FirstOrDefault().CF_TeachingMaterialPool.ISBN,
  245. //TeachingMaterialTypeName = ed.Name,
  246. PublishTime = tea.FirstOrDefault().CF_TeachingMaterialPool.PublishTime,
  247. PublishName = ppp.UnitName,
  248. Author = tea.FirstOrDefault().CF_TeachingMaterialPool.Author,
  249. InventoryCollectQty = (tea.Sum(x => x.PresentInventory) ?? 0) - (InByOuttb.InQty ?? 0) + (InByOuttb.OutQty ?? 0),
  250. ////StockInQty = InByOuttb.InQty == null ? 0 : InByOuttb.InQty,
  251. ////StockOutQty = InByOuttb.OutQty == null ? 0 : InByOuttb.OutQty,
  252. StockInQty = ctoldstin.oldInQty ?? 0,
  253. StockOutQty = ctoldstin.oldOutQty ?? 0,
  254. PresentInventory = tea.Sum(x => x.PresentInventory) ?? 0,
  255. MinInventory = tea.FirstOrDefault().CF_TeachingMaterialPool.MinInventory == null ? 1 : tea.FirstOrDefault().CF_TeachingMaterialPool.MinInventory,
  256. Price = tea.FirstOrDefault().CF_TeachingMaterialPool.Price,
  257. };
  258. #endregion
  259. return InventoryViews;
  260. }
  261. #region 2.0 获取库存统计信息
  262. /// <summary>
  263. /// 获取教材库存信息列表
  264. /// </summary>
  265. /// <param name="exp"></param>
  266. /// <returns></returns>
  267. public IQueryable<InventoryView> GetInventoryManagementGridView(Guid? schoolyearID)
  268. {
  269. int? yearValue = 0;
  270. string YearCode = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Code;
  271. if (schoolyearID != null)
  272. {
  273. yearValue = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Value;
  274. }
  275. #region 0.0 找出当前学年的出入库数量
  276. var oldStockIns = from a in StockInRepository.Entities.Where(x => x.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  277. join b in StockInDetailRepository.Entities on a.StockInID equals b.StockInID
  278. group b by new {b.TeachingMaterialPoolID,b.DiscountPrice}
  279. into stockin
  280. select new
  281. {
  282. TeachingMaterialPoolID = (Guid)stockin.Key.TeachingMaterialPoolID,
  283. DiscountPrice = stockin.Key.DiscountPrice,//折合价
  284. InQty = stockin.Sum(x => x.Quantity),
  285. OutQty = (int?)0,
  286. };
  287. var oldStockOuts = from a in StockOutRepository.Entities.Where(x => x.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  288. join b in StockOutDetailRepository.Entities on a.StockOutID equals b.StockOutID
  289. group b by new { b.TeachingMaterialPoolID, b.Discount }
  290. into stockout
  291. select new
  292. {
  293. TeachingMaterialPoolID = stockout.Key.TeachingMaterialPoolID,
  294. DiscountPrice = stockout.Key.Discount,//折合价
  295. InQty = (int?)0,
  296. OutQty = (int?)stockout.Sum(x => x.StockOutQuantity),
  297. };
  298. //var oldStudentDistributes = from a in StudentDistributeRepository.Entities.Where(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  299. // join b in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals b.TeachingMaterialPoolID
  300. // into tea
  301. // from ter in tea.DefaultIfEmpty()
  302. // select new
  303. // {
  304. // TeachingMaterialPoolID = (Guid)a.TeachingMaterialPoolID,
  305. // DiscountPrice = ter.Discount,
  306. // // SchoolyearID = a.SchoolyearID,
  307. // OutQty = (int)a.DistributeQty - a.CF_Student.Count()
  308. // } into c
  309. // group c by new { c.TeachingMaterialPoolID, c.DiscountPrice }
  310. // into d
  311. // select new
  312. // {
  313. // TeachingMaterialPoolID = d.Key.TeachingMaterialPoolID,
  314. // DiscountPrice = d.Key.DiscountPrice,
  315. // // SchoolyearID = a.SchoolyearID,
  316. // InQty = (int?)0,
  317. // OutQty = (int?)d.Sum(x => x == null ? 0 : x.OutQty),
  318. // };
  319. var oldStockInQry = //from b in oldStockIns.Concat(oldStockOuts).Concat(oldStudentDistributes)
  320. from b in oldStockIns
  321. join c in oldStockOuts on new { b.TeachingMaterialPoolID, b.DiscountPrice } equals new { c.TeachingMaterialPoolID, c.DiscountPrice }
  322. into cc
  323. from ccc in cc.DefaultIfEmpty()
  324. select new
  325. {
  326. TeachingMaterialPoolID = (Guid)b.TeachingMaterialPoolID,
  327. DiscountPrice = b.DiscountPrice,
  328. // SchoolyearID = h.Key.SchoolyearID,
  329. oldInQty = b.InQty,
  330. oldOutQty = ccc.OutQty ?? 0,
  331. };
  332. var oldInAndOut = from b in oldStockIns.Concat(oldStockOuts)
  333. group b by new { b.TeachingMaterialPoolID, b.DiscountPrice }
  334. into aa
  335. select new
  336. {
  337. TeachingMaterialPoolID = (Guid)aa.Key.TeachingMaterialPoolID,
  338. DiscountPrice = aa.Key.DiscountPrice,
  339. // SchoolyearID = h.Key.SchoolyearID,
  340. oldInQty = aa.Sum(x => x.InQty),
  341. oldOutQty = aa.Sum(x => x.OutQty),
  342. };
  343. #endregion
  344. #region 0.1 找出当前学年之后的出入库数量汇总(包含当前学年) 列: >=2015-201601 之后
  345. #region 1.0 入库
  346. var StockInview = from a in StockInRepository.Entities.Where(x => x.CF_Schoolyear.Value >= yearValue && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  347. join b in StockInDetailRepository.Entities on a.StockInID equals b.StockInID
  348. group b by new { b.TeachingMaterialPoolID, b.DiscountPrice }
  349. into stockin
  350. select new
  351. {
  352. TeachingMaterialPoolID = (Guid)stockin.Key.TeachingMaterialPoolID,
  353. DiscountPrice = stockin.Key.DiscountPrice,//折合价
  354. InQty = stockin.Sum(x => x.Quantity),
  355. OutQty = (int?)0
  356. };
  357. #endregion
  358. #region 2.0 出库
  359. var StockOutview = from a in StockOutRepository.Entities.Where(x => x.CF_Schoolyear.Value >= yearValue && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  360. join b in StockOutDetailRepository.Entities on a.StockOutID equals b.StockOutID
  361. group b by new { b.TeachingMaterialPoolID, b.Discount }
  362. into stockout
  363. select new
  364. {
  365. TeachingMaterialPoolID = stockout.Key.TeachingMaterialPoolID,
  366. DiscountPrice = stockout.Key.Discount,//折合价
  367. InQty = (int?)0,
  368. OutQty = stockout.Sum(x => x.StockOutQuantity),
  369. };
  370. #endregion
  371. #region 2.0 学生发放
  372. //var StudentDistributeview = from a in StudentDistributeRepository.Entities.Where(x => x.EM_SpecialtyPlan.CF_Schoolyear.Value.Value >= yearValue && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  373. // join b in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals b.TeachingMaterialPoolID
  374. // into tea
  375. // from ter in tea.DefaultIfEmpty()
  376. // select new
  377. // {
  378. // TeachingMaterialPoolID = (Guid)a.TeachingMaterialPoolID,
  379. // DiscountPrice = ter.Discount,
  380. // // SchoolyearID = a.SchoolyearID,
  381. // OutQty = (int)a.DistributeQty - a.CF_Student.Count()
  382. // } into c
  383. // group c by new { c.TeachingMaterialPoolID, c.DiscountPrice }
  384. // into d
  385. // select new
  386. // {
  387. // TeachingMaterialPoolID = d.Key.TeachingMaterialPoolID,
  388. // DiscountPrice = d.Key.DiscountPrice,
  389. // // SchoolyearID = a.SchoolyearID,
  390. // OutQty = d.Sum(x => x == null ? 0 : x.OutQty)
  391. // };
  392. #endregion
  393. #region 3.0 合并出入库数据进行分组
  394. var InByOutStockInfoGroup = from b in oldStockIns
  395. //join d in oldStudentDistributes on new { b.TeachingMaterialPoolID, b.DiscountPrice } equals new { d.TeachingMaterialPoolID, d.DiscountPrice }
  396. //into dd
  397. //from ddd in dd.DefaultIfEmpty()
  398. join c in oldStockOuts on new { b.TeachingMaterialPoolID, b.DiscountPrice } equals new { c.TeachingMaterialPoolID, c.DiscountPrice }
  399. into cc
  400. from ccc in cc.DefaultIfEmpty()
  401. select new
  402. {
  403. TeachingMaterialPoolID = (Guid)b.TeachingMaterialPoolID,
  404. DiscountPrice = b.DiscountPrice,
  405. // SchoolyearID = h.Key.SchoolyearID,
  406. InQty = b.InQty,
  407. OutQty = ccc.OutQty ?? 0 ,
  408. };
  409. var InAndOut = from b in StockInview.Concat(StockOutview)
  410. group b by new { b.TeachingMaterialPoolID, b.DiscountPrice }
  411. into aa
  412. select new
  413. {
  414. TeachingMaterialPoolID = (Guid)aa.Key.TeachingMaterialPoolID,
  415. DiscountPrice = aa.Key.DiscountPrice,
  416. // SchoolyearID = h.Key.SchoolyearID,
  417. InQty = aa.Sum(x => x.InQty),
  418. OutQty = aa.Sum(x => x.OutQty),
  419. };
  420. #endregion
  421. #endregion
  422. #region 4.0 对分组后的出入库数据跟教材库存做统计输出~
  423. var InventoryViews = from a in TeachingMaterialPoolRepository.Entities.Where(x => x.IsLate == false)
  424. join t in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals t.TeachingMaterialPoolID
  425. into tsct
  426. from tm in tsct.DefaultIfEmpty()
  427. join InByOut in InAndOut on new { tm.TeachingMaterialPoolID, DiscountPrice = tm.Discount } equals new { InByOut.TeachingMaterialPoolID, DiscountPrice = InByOut.DiscountPrice }
  428. into InByOutdata
  429. from InByOuttb in InByOutdata.DefaultIfEmpty()
  430. join oldstin in oldInAndOut on new { tm.TeachingMaterialPoolID, DiscountPrice = tm.Discount } equals new { oldstin.TeachingMaterialPoolID, DiscountPrice = oldstin.DiscountPrice }
  431. into toldstin
  432. from ctoldstin in toldstin.DefaultIfEmpty()
  433. //join oldin in oldStockIns on new { tm.TeachingMaterialPoolID, DiscountPrice = tm.Discount } equals new { oldin.TeachingMaterialPoolID, DiscountPrice = oldin.DiscountPrice }
  434. //join outs in oldStockOuts on new { tm.TeachingMaterialPoolID, DiscountPrice = tm.Discount } equals new { outs.TeachingMaterialPoolID, DiscountPrice = outs.DiscountPrice }
  435. //into sout
  436. //from st in sout.DefaultIfEmpty()
  437. join p in PublishRepository.Entities on a.PublishID equals p.PublishID
  438. into pp
  439. from ppp in pp.DefaultIfEmpty()
  440. //join c in CoursematerialRepository.Entities on a.CoursematerialID equals c.CoursematerialID
  441. //join d in DictionaryItemRepository.Entities
  442. // on new { a.TeachingMaterialTypeID, DictionaryCode = DictionaryItem.CF_TeachingMaterialType.ToString() } equals new { TeachingMaterialTypeID = d.Value, d.DictionaryCode }
  443. //into dd
  444. //from ed in dd.DefaultIfEmpty()
  445. //join oldStockin in oldStockIns on tm.TeachingMaterialPoolID equals oldStockin.TeachingMaterialPoolID
  446. //into d
  447. //from dd in d.DefaultIfEmpty()
  448. //join oldStockout in oldStockOuts on tm.TeachingMaterialPoolID equals oldStockout.TeachingMaterialPoolID
  449. //into f
  450. //from ff in f.DefaultIfEmpty()
  451. // join oldout in oldStockOut.Concat(oldStudentDistribute) on tm.TeachingMaterialPoolID equals oldout.TeachingMaterialPoolID
  452. //into toldout
  453. // from ctoldout in toldout.DefaultIfEmpty()
  454. select new InventoryView
  455. {
  456. SchoolYearName = YearCode,
  457. TeachingMaterialPoolID = a.TeachingMaterialPoolID,
  458. // SchoolYearID = InByOuttb.SchoolyearID == null ? (Guid)schoolyearID : InByOuttb.SchoolyearID,
  459. PublishID = a.PublishID,
  460. TeachingMaterialTypeID = a.TeachingMaterialTypeID,
  461. TeachingMaterialCode = a.TeachingMaterialCode,
  462. TeachingMaterialName = a.TeachingMaterialName,
  463. CoursematerialEntityList = a.EM_Coursematerial,
  464. ISBN = a.ISBN,
  465. //TeachingMaterialTypeName = ed.Name,
  466. PublishTime = a.PublishTime,
  467. PublishName = ppp.UnitName,
  468. Author = a.Author,
  469. InventoryCollectQty = (tm.PresentInventory ?? 0) - (InByOuttb.InQty ?? 0) + (InByOuttb.OutQty ?? 0),
  470. ////StockInQty = InByOuttb.InQty == null ? 0 : InByOuttb.InQty,
  471. ////StockOutQty = InByOuttb.OutQty == null ? 0 : InByOuttb.OutQty,
  472. StockInQty = ctoldstin.oldInQty ?? 0,
  473. StockOutQty = ctoldstin.oldOutQty ?? 0,
  474. PresentInventory = tm.PresentInventory ?? 0,
  475. MinInventory = a.MinInventory == null ? 1 : a.MinInventory,
  476. Price = a.Price,
  477. Discount = (tm.Discount / a.Price) == null ? 1 : Math.Round((decimal)(tm.Discount / a.Price), 2),
  478. DiscountPrice = tm.Discount ?? 0,
  479. Total = (a.Price * tm.PresentInventory) == null ? 0 : (a.Price * tm.PresentInventory),
  480. TotalDollar = (tm.Discount * tm.PresentInventory) == null ? 0 : (tm.Discount * tm.PresentInventory)
  481. };
  482. #endregion
  483. return InventoryViews;
  484. }
  485. #endregion
  486. #region 3.0 出库明细信息列表
  487. public IQueryable<StockOutView> GetStockOutDetailGridView(Guid? schoolyearID, Guid? teachingMaterialPoolID)
  488. {
  489. int? yearValue = 0;
  490. if (schoolyearID != null)
  491. {
  492. yearValue = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Value;
  493. }
  494. string code = SchoolyearRepository.GetSingle(x => x.SchoolyearID == schoolyearID).Code;
  495. #region 1.0 出库
  496. var StockOutview = from a in StockOutRepository.Entities.Where(x => x.CF_Schoolyear.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes)
  497. join b in StockOutDetailRepository.Entities.Where(x => x.TeachingMaterialPoolID == teachingMaterialPoolID) on a.StockOutID equals b.StockOutID
  498. select new
  499. {
  500. TeachingMaterialPoolID = (Guid)b.TeachingMaterialPoolID,
  501. StockOutNo = a.StockOutNo,
  502. OutQty = (int)b.StockOutQuantity,
  503. StockOutTime = a.StockOutTime,
  504. StockOutType = (int)a.StockOutType,
  505. RecipientUserID = a.RecipientUserID == null ? Guid.Empty : a.RecipientUserID,
  506. QianShouRen = a.GetBookNo,
  507. DiscountPrice = b.Discount,//折合价
  508. Discount = b.DiscountPrice,//折扣率
  509. Desc = a.Desc,
  510. };
  511. #endregion
  512. //#region 2.0 学生发放
  513. //var StudentDistributeview = from a in StudentDistributeRepository.Entities.Where(x => x.EM_SpecialtyPlan.CF_Schoolyear.SchoolyearID == schoolyearID && x.RecordStatus == (int)CF_YesOrNoStatus.Yes && x.TeachingMaterialPoolID == teachingMaterialPoolID)
  514. // select new
  515. // {
  516. // TeachingMaterialPoolID = (Guid)a.TeachingMaterialPoolID,
  517. // StockOutNo = a.StudentDistributeNo,
  518. // OutQty = (int)a.DistributeQty - a.CF_Student.Count(),
  519. // StockOutTime = a.ModifyTime,
  520. // StockOutType = (int)a.StockOutType,
  521. // RecipientUserID = a.ModifyUserID,
  522. // QianShouRen = a.RecipientUser,
  523. // Desc = a.Remark,
  524. // };
  525. //#endregion
  526. var query = from a in StockOutview
  527. join c in UserRepository.Entities on a.RecipientUserID equals c.UserID
  528. into cuser
  529. from tcuser in cuser.DefaultIfEmpty()
  530. join t in TeachingMaterialPoolRepository.Entities on a.TeachingMaterialPoolID equals t.TeachingMaterialPoolID
  531. //join tp in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals tp.TeachingMaterialPoolID
  532. select new StockOutView
  533. {
  534. SchoolyearName = code,
  535. StockOutNo = a.StockOutNo,
  536. GetBookNo = a.QianShouRen.ToString(),
  537. TeachingMaterialCode = t.TeachingMaterialCode,
  538. TeachingMaterialName = t.TeachingMaterialName,
  539. TeachingMaterialPoolID = t.TeachingMaterialPoolID,
  540. PublishName = t.CF_Publish.UnitName,
  541. PublishTime = t.PublishTime,
  542. Author = t.Author,
  543. Quantity = a.OutQty,
  544. StockOutTypeID = a.StockOutType,//出库类型
  545. Price = t.Price,
  546. Discount = (a.Discount == null || a.Discount == 0) ? (a.DiscountPrice / t.Price) : a.Discount,
  547. DiscountPrice = a.DiscountPrice,
  548. TotalPrice = t.Price * a.OutQty,
  549. TotalDollar = a.DiscountPrice * a.OutQty,
  550. RecipientUserName = tcuser.Name == null ? a.QianShouRen : tcuser.Name,
  551. StockOutTime = a.StockOutTime,
  552. Desc = a.Desc
  553. };
  554. return query;
  555. }
  556. public IQueryable<StockOutView> GetStudentDistributeDetailGridView(Expression<Func<ET_StudentDistribute, bool>> exporder)
  557. {
  558. var viewList = (from a in StudentDistributeRepository.GetList(exporder)
  559. join t in TeachingMaterialPoolRepository.Entities on a.TeachingMaterialPoolID equals t.TeachingMaterialPoolID
  560. into ct
  561. from sct in ct.DefaultIfEmpty()
  562. join tp in TeachingMateriaInventoryRepository.Entities on a.TeachingMaterialPoolID equals tp.TeachingMaterialPoolID
  563. into ctp
  564. from sctp in ctp.DefaultIfEmpty()
  565. select new StockOutView
  566. {
  567. TeachingMaterialPoolID = (Guid)a.TeachingMaterialPoolID,
  568. StockOutDetailID = Guid.Empty,
  569. SchoolyearName = a.EM_SpecialtyPlan.CF_Schoolyear.Code,
  570. SchoolyearID = a.EM_SpecialtyPlan.CF_Schoolyear.SchoolyearID,
  571. StockOutNo = a.StudentDistributeNo,
  572. GetBookNo = a.RecipientUser.ToString(),
  573. TeachingMaterialCode = sct.TeachingMaterialCode,
  574. TeachingMaterialName = sct.TeachingMaterialName,
  575. PublishName = sct.CF_Publish.UnitName,
  576. PublishTime = sct.PublishTime,
  577. Author = sct.Author,
  578. Quantity = a.DistributeQty,
  579. Price = sct.Price,
  580. Discount = sctp.Discount / sct.Price,
  581. DiscountPrice = sctp.Discount,
  582. TotalPrice = sct.Price * a.DistributeQty,
  583. TotalDollar = (sct.Price * (sctp.Discount / sct.Price)) * a.DistributeQty,
  584. RecipientUserName = a.RecipientUser.ToString(),
  585. StockOutID = a.StudentDistributeID,
  586. StockOutTime = a.ModifyTime,
  587. Desc = a.Remark
  588. });
  589. return viewList;
  590. }
  591. #endregion
  592. #region 4.0 入库明细信息列表
  593. public IQueryable<StockInView> GetStockInDetailGridView(Expression<Func<ET_StockIn, bool>> exporder)
  594. {
  595. var viewList = (from a in StockInRepository.GetList(exporder)
  596. join b in SchoolyearRepository.Entities on a.SchoolyearID equals b.SchoolyearID
  597. join c in UserRepository.Entities on a.HandlerUserID equals c.UserID
  598. into cuser
  599. from tcuser in cuser.DefaultIfEmpty()
  600. join d in PublishRepository.Entities on a.SupplierID equals d.PublishID
  601. into dTmp
  602. from publishTab in dTmp.DefaultIfEmpty()
  603. join e in StockInDetailRepository.Entities on a.StockInID equals e.StockInID
  604. join t in TeachingMaterialPoolRepository.Entities on e.TeachingMaterialPoolID equals t.TeachingMaterialPoolID
  605. select new StockInView
  606. {
  607. TeachingMaterialPoolID = (Guid)e.TeachingMaterialPoolID,
  608. StockInDetailID = e.StockInDetailID,
  609. SchoolyearName = b.Code,
  610. SchoolyearID = (Guid)a.SchoolyearID,
  611. SupplierID = a.SupplierID,
  612. StockInDocumentNo = a.StockInDocumentNo,
  613. StockInNumber = a.StockInNumber.ToString(),
  614. TeachingMaterialCode = t.TeachingMaterialCode,
  615. TeachingMaterialName = t.TeachingMaterialName,
  616. PublishName = t.CF_Publish.UnitName,
  617. PublishTime = t.PublishTime,
  618. Author = t.Author,
  619. Quantity = e.Quantity,
  620. Price = t.Price,
  621. Discount = e.Discount,
  622. DiscountPrice = e.DiscountPrice,
  623. TotalPrice = t.Price * e.Quantity,
  624. TotalDollar = e.DiscountPrice * e.Quantity,
  625. HandlerUserName = tcuser.Name == null ? "" : tcuser.Name,
  626. StockInUserName = tcuser.Name == null ? "" : tcuser.Name,
  627. StockInID = a.StockInID,
  628. StockInTime = a.StockInTime
  629. });
  630. return viewList;
  631. }
  632. #endregion
  633. }
  634. }