StatementBillsApplicationService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using System.Threading.Tasks;
  7. using System.Web.Mvc;
  8. using Abp.Application.Services.Dto;
  9. using Abp.Auditing;
  10. using Abp.Authorization;
  11. using Abp.Domain.Repositories;
  12. using Abp.Json;
  13. using Abp.Runtime.Caching;
  14. using Abp.Timing;
  15. using IwbZero.Auditing;
  16. using IwbZero.AppServiceBase;
  17. using IwbZero.Helper;
  18. using JetBrains.Annotations;
  19. using ShwasherSys.Authorization.Permissions;
  20. using ShwasherSys.BaseSysInfo;
  21. using ShwasherSys.Common;
  22. using ShwasherSys.CustomerInfo;
  23. using ShwasherSys.Invoice.Dto;
  24. using ShwasherSys.OrderSendInfo;
  25. using ShwasherSys.OrderSendInfo.Dto;
  26. using ShwasherSys.EntityFramework;
  27. using ShwasherSys.ProductStoreInfo.Dto;
  28. namespace ShwasherSys.Invoice
  29. {
  30. [AbpAuthorize]
  31. public class StatementBillAppService : IwbZeroAsyncCrudAppService<StatementBill, StatementBillDto, int, IwbPagedRequestDto, StatementBillCreateDto, StatementBillUpdateDto >, IStatementBillAppService
  32. {
  33. public StatementBillAppService(
  34. ICacheManager cacheManager,
  35. IRepository<StatementBill, int> repository, IRepository<ViewOrderSend> viewOrderSendRepository, ISqlExecuter sqlExecuter, IRepository<OrderSend> orderSendRepository,IRepository<ViewStatementBill> viewStatementBillRepository, IRepository<Customer, string> customerRepository) : base(repository, "StatementBillNo")
  36. {
  37. ViewOrderSendRepository = viewOrderSendRepository;
  38. CacheManager = cacheManager;
  39. SqlExecuter = sqlExecuter;
  40. _viewStatementBillRepository = viewStatementBillRepository;
  41. CustomerRepository = customerRepository;
  42. OrderSendRepository = orderSendRepository;
  43. }
  44. protected ISqlExecuter SqlExecuter;
  45. private readonly IRepository<ViewStatementBill> _viewStatementBillRepository;
  46. protected override bool KeyIsAuto { get; set; } = false;
  47. protected IRepository<ViewOrderSend> ViewOrderSendRepository { get; }
  48. protected IRepository<OrderSend> OrderSendRepository { get; }
  49. protected IRepository<Customer,string> CustomerRepository { get; }
  50. #region GetSelect
  51. [DisableAuditing]
  52. public override async Task<List<SelectListItem>> GetSelectList()
  53. {
  54. var list = await Repository.GetAllListAsync();
  55. var sList = new List<SelectListItem> {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}};
  56. foreach (var l in list)
  57. {
  58. //sList.Add(new SelectListItem { Value = l.Id, Text = l. });
  59. }
  60. return sList;
  61. }
  62. [DisableAuditing]
  63. public override async Task<string> GetSelectStr()
  64. {
  65. var list = await Repository.GetAllListAsync();
  66. string str = "<option value=\"\" selected>请选择...</option>";
  67. foreach (var l in list)
  68. {
  69. //str += $"<option value=\"{l.Id}\">{l.}</option>";
  70. }
  71. return str;
  72. }
  73. #endregion
  74. #region CURD
  75. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillCreate)]
  76. public override async Task Create(StatementBillCreateDto input)
  77. {
  78. string orderSendIds = input.OrderSendIds;
  79. //string lcSql = "update OrderSend set StatementBillNo='" + input.StatementBillNo + "' ";
  80. //lcSql += " where OrderSendId in(" + orderSendIds + ")";
  81. //var sCount = SqlExecuter.Execute(lcSql);
  82. string lcSql = "";
  83. var osi = Array.ConvertAll<string, int>(orderSendIds.Split(','), int.Parse);
  84. for (int i = 0; i < osi.Length; i++)
  85. {
  86. lcSql += $"update OrderSend set StatementBillNo='{input.StatementBillNo}',StatementBillSort=${i + 1} where OrderSendId = ${osi[i]};\r\n ";
  87. }
  88. var sCount = SqlExecuter.Execute(lcSql);
  89. input.BillMan = AbpSession.UserName;
  90. input.StatementState = 0;
  91. await CreateEntity(input);
  92. await CurrentUnitOfWork.SaveChangesAsync();
  93. }
  94. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  95. public override async Task Update(StatementBillUpdateDto input)
  96. {
  97. await UpdateEntity(input);
  98. }
  99. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillUpdateDesc), AuditLog("修改对账单备注")]
  100. public async Task UpdateDesc(StatementBillUpdateDescDto input)
  101. {
  102. var entity = Repository.Get(input.Id);
  103. entity.Description = input.Description;
  104. this.Logger.Info("修改对账单备注!" + entity.ToJsonString());
  105. await Repository.UpdateAsync(entity);
  106. }
  107. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillDelete)]
  108. public override async Task Delete(EntityDto<int> input)
  109. {
  110. var entity = Repository.Get(input.Id);
  111. var orderSends = OrderSendRepository.GetAllList(i => i.StatementBillNo == entity.StatementBillNo);
  112. foreach (var send in orderSends)
  113. {
  114. send.StatementBillNo = "";
  115. send.TimeLastMod = Clock.Now;
  116. send.UserIDLastMod = AbpSession.UserName;
  117. OrderSendRepository.Update(send);
  118. }
  119. //BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository, "撤销对账单", $"对账单{entity.ToJsonString()}撤销");
  120. //this.Logger.Info("撤销发票!" + entity.ToString());
  121. await Repository.DeleteAsync(input.Id);
  122. }
  123. [DisableAuditing]
  124. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  125. public override async Task<PagedResultDto<StatementBillDto>> GetAll(IwbPagedRequestDto input)
  126. {
  127. var query = CreateFilteredQuery(input);
  128. var customers = CustomerRepository.GetAll();
  129. var result = query.Join(customers, s => s.CustomerId, c => c.Id, (s, c) => new StatementBillDto(){CustomerId=s.CustomerId,CustomerName=c.CustomerName,BillMan = s.BillMan,Description=s.Description,Id=s.Id, OrderStickBillNo=s.OrderStickBillNo,StatementBillNo=s.StatementBillNo, StatementState = s.StatementState,
  130. CreationTime=s.CreationTime
  131. });
  132. result = ApplyFilter(result, input);
  133. var totalCount = await AsyncQueryableExecuter.CountAsync(result);
  134. result = result.OrderByDescending(i => i.CreationTime);
  135. result = result.Skip(input.SkipCount).Take(input.MaxResultCount);
  136. var dtoList = new PagedResultDto<StatementBillDto>(totalCount, result.ToList());
  137. return dtoList;
  138. }
  139. #region GetEntity/Dto
  140. /// <summary>
  141. /// 查询实体Dto
  142. /// </summary>
  143. /// <param name="input"></param>
  144. /// <returns></returns>
  145. [DisableAuditing]
  146. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  147. public override async Task<StatementBillDto> GetDto(EntityDto<int> input)
  148. {
  149. var entity = await GetEntity(input);
  150. return MapToEntityDto(entity);
  151. }
  152. /// <summary>
  153. /// 查询实体Dto
  154. /// </summary>
  155. /// <param name="id"></param>
  156. /// <returns></returns>
  157. [DisableAuditing]
  158. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  159. public override async Task<StatementBillDto> GetDtoById(int id)
  160. {
  161. var entity = await GetEntityById(id);
  162. return MapToEntityDto(entity);
  163. }
  164. /// <summary>
  165. /// 查询实体Dto(需指明自定义字段)
  166. /// </summary>
  167. /// <param name="no"></param>
  168. /// <returns></returns>
  169. [DisableAuditing]
  170. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  171. public override async Task<StatementBillDto> GetDtoByNo(string no)
  172. {
  173. var entity = await GetEntityByNo(no);
  174. return MapToEntityDto(entity);
  175. }
  176. /// <summary>
  177. /// 查询实体
  178. /// </summary>
  179. /// <param name="input"></param>
  180. /// <returns></returns>
  181. [DisableAuditing]
  182. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  183. public override async Task<StatementBill> GetEntity(EntityDto<int> input)
  184. {
  185. var entity = await GetEntityById(input.Id);
  186. return entity;
  187. }
  188. /// <summary>
  189. /// 查询实体
  190. /// </summary>
  191. /// <param name="id"></param>
  192. /// <returns></returns>
  193. [DisableAuditing]
  194. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  195. public override async Task<StatementBill> GetEntityById(int id)
  196. {
  197. return await Repository.FirstOrDefaultAsync(a=>a.Id==id);
  198. }
  199. /// <summary>
  200. /// 查询实体(需指明自定义字段)
  201. /// </summary>
  202. /// <param name="no"></param>
  203. /// <returns></returns>
  204. [DisableAuditing]
  205. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)]
  206. public override async Task<StatementBill> GetEntityByNo(string no)
  207. {
  208. //CheckGetPermission();
  209. if (string.IsNullOrEmpty(KeyFiledName))
  210. {
  211. ThrowError("NoKeyFieldName");
  212. }
  213. return await base.GetEntityByNo(no);
  214. }
  215. #endregion
  216. #region Hide
  217. ///// <summary>
  218. ///// 根据给定的<see cref="IwbPagedRequestDto"/>创建 <see cref="IQueryable{StatementBill}"/>过滤查询.
  219. ///// </summary>
  220. ///// <param name="input">The input.</param>
  221. //protected override IQueryable<StatementBill> CreateFilteredQuery(IwbPagedRequestDto input)
  222. //{
  223. // var query = Repository.GetAll();
  224. // var pagedInput = input as IIwbPagedRequest;
  225. // if (pagedInput == null)
  226. // {
  227. // return query;
  228. // }
  229. // if (!string.IsNullOrEmpty(pagedInput.KeyWords))
  230. // {
  231. // object keyWords = pagedInput.KeyWords;
  232. // LambdaObject obj = new LambdaObject()
  233. // {
  234. // FieldType = (LambdaFieldType)pagedInput.FieldType,
  235. // FieldName = pagedInput.KeyField,
  236. // FieldValue = keyWords,
  237. // ExpType = (LambdaExpType)pagedInput.ExpType
  238. // };
  239. // var exp = obj.GetExp<StatementBill>();
  240. // query = exp != null ? query.Where(exp) : query;
  241. // }
  242. // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0)
  243. // {
  244. // List<LambdaObject> objList = new List<LambdaObject>();
  245. // foreach (var o in pagedInput.SearchList)
  246. // {
  247. // if (string.IsNullOrEmpty(o.KeyWords))
  248. // continue;
  249. // object keyWords = o.KeyWords;
  250. // objList.Add(new LambdaObject
  251. // {
  252. // FieldType = (LambdaFieldType)o.FieldType,
  253. // FieldName = o.KeyField,
  254. // FieldValue = keyWords,
  255. // ExpType = (LambdaExpType)o.ExpType
  256. // });
  257. // }
  258. // var exp = objList.GetExp<StatementBill>();
  259. // query = exp != null ? query.Where(exp) : query;
  260. // }
  261. // return query;
  262. //}
  263. //protected override IQueryable<StatementBill> ApplySorting(IQueryable<StatementBill> query, IwbPagedRequestDto input)
  264. //{
  265. // return query.OrderBy(a => a.No);
  266. //}
  267. //protected override IQueryable<StatementBill> ApplyPaging(IQueryable<StatementBill> query, IwbPagedRequestDto input)
  268. //{
  269. // if (input is IPagedResultRequest pagedInput)
  270. // {
  271. // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount);
  272. // }
  273. // return query;
  274. //}
  275. #endregion
  276. #endregion
  277. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill), AuditLog("查询出待生成对账单的客户列表记录")]
  278. public List<SelectListItem> GetHasSendOrderCustomer()
  279. {
  280. List<SelectListItem> listItems = new List<SelectListItem>();
  281. var queryAllList = ViewOrderSendRepository.GetAll().Where(i => (i.OrderSendBillNo != null && i.OrderSendBillNo != "") && i.CustomerId != null&& (i.StatementBillNo == null || i.StatementBillNo == "")).Select(i => i.CustomerId).Distinct();
  282. foreach (var send in queryAllList)
  283. {
  284. listItems.Add(new SelectListItem()
  285. {
  286. Text = send,
  287. Value = send
  288. });
  289. }
  290. return listItems;
  291. }
  292. [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill), AuditLog("根据客户编号,查询出待生成对账单的发货明细")]
  293. public async Task<List<ViewOrderSend>> GetOrderSendByCustomerId(QuerySendDto input)
  294. {
  295. var queryAllList = ViewOrderSendRepository.GetAll().Where(i =>
  296. i.CustomerId == input.CustomerId && (i.OrderSendBillNo != null && i.OrderSendBillNo != "") &&
  297. (i.StatementBillNo == ""|| i.StatementBillNo == null));//.OrderByDescending(i=>i.SendActionDate).ThenBy(i=>i.ProductName);
  298. if (input.SendDateStart != null && input.SendDateStart > DateTime.MinValue)
  299. {
  300. queryAllList = queryAllList.Where(i => i.SendActionDate >= input.SendDateStart);
  301. }
  302. if (input.SendDateEnd != null && input.SendDateEnd > DateTime.MinValue)
  303. {
  304. queryAllList = queryAllList.Where(i => i.SendActionDate <= input.SendDateEnd);
  305. }
  306. //queryAllList = queryAllList.OrderByDescending(i => i.SendActionDate).ThenBy(i => i.ProductName);
  307. queryAllList = queryAllList.OrderBy(i => i.SendActionDate).ThenBy(i => i.ProductName);
  308. //queryAllList = queryAllList.OrderByDescending(i => i.CustomerId).ThenBy(i => i.SendBillSort);
  309. return await AsyncQueryableExecuter.ToListAsync(queryAllList);
  310. }
  311. public async Task<List<ViewStatementBill>> QueryStatisticStatementBillItems(QueryStatementBillReportDto input)
  312. {
  313. var startDate = input.Year.GetDateByType(input.Month, out var endDate, out var dateStr);
  314. //查询外购的排产单进行统计(不是新建状态的)
  315. var query = _viewStatementBillRepository.GetAll().Where(a => a.CreationTime >= startDate && a.CreationTime < endDate);
  316. if (!string.IsNullOrEmpty(input.CustomerId))
  317. {
  318. query = query.Where(i =>i.CustomerId==input.CustomerId);
  319. }
  320. var items = await query.OrderByDescending(i => i.CreationTime).ToListAsync();
  321. return items;
  322. }
  323. }
  324. }