OrderStickBillsApplicationService.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Linq.Dynamic.Core;
  7. using System.Threading.Tasks;
  8. using Abp.Application.Services.Dto;
  9. using Abp.Authorization;
  10. using Abp.Collections.Extensions;
  11. using Abp.Domain.Repositories;
  12. using Abp.Extensions;
  13. using Abp.Json;
  14. using Abp.Timing;
  15. using Abp.UI;
  16. using IwbZero.AppServiceBase;
  17. using IwbZero.Auditing;
  18. using IwbZero.IdentityFramework;
  19. using Microsoft.AspNet.SignalR.Hubs;
  20. using ShwasherSys.Authorization.Permissions;
  21. using ShwasherSys.BaseSysInfo;
  22. using ShwasherSys.Common;
  23. using ShwasherSys.CustomerInfo;
  24. using ShwasherSys.EntityFramework;
  25. using ShwasherSys.Invoice.Dto;
  26. using ShwasherSys.Lambda;
  27. using ShwasherSys.OrderSendInfo;
  28. using ShwasherSys.OrderSendInfo.Dto;
  29. using ShwasherSys.ProductInfo;
  30. namespace ShwasherSys.Invoice
  31. {
  32. [AbpAuthorize]
  33. public class OrderStickBillAppService : ShwasherAsyncCrudAppService<OrderStickBill, OrderStickBillDto, string, PagedRequestDto, OrderStickBillCreateDto, OrderStickBillUpdateDto >, IOrderStickBillAppService
  34. {
  35. protected IRepository<ViewCustomerStick> ViewCustomerStickRepository;
  36. protected IRepository<ViewOrderSendStickBill> ViewOrderSendStickBillRepository;
  37. public IRepository<OrderSendBill, string> OrderSendBillRepository;
  38. protected IRepository<Customer,string> CustomerRepository;
  39. protected IRepository<OrderSend> OrderSendRepository;
  40. protected ISqlExecuter SqlExecute;
  41. protected IRepository<BusinessLog> BusinessLogRepository { get; }
  42. protected IRepository<ViewStickBill,string> ViewStickBillRepository { get; }
  43. protected IRepository<ViewStatementBill> ViewStatementBillRepository { get; }
  44. protected IQueryAppService QueryAppService { get; }
  45. public OrderStickBillAppService(IRepository<OrderStickBill, string> repository, IRepository<ViewCustomerStick> viewCustomerStickRepository, ISqlExecuter sqlExecute, IRepository<Customer, string> customerRepository, IRepository<OrderSend> orderSendRepository, IRepository<BusinessLog> businessLogRepository, IRepository<ViewStickBill, string> viewStickBillRepository,IRepository<ViewOrderSendStickBill> viewOrderSendStickBillRepository, IRepository<OrderSendBill, string> orderSendBillRepository, IRepository<ViewStatementBill> viewStatementBillRepository,IQueryAppService queryAppService) : base(repository)
  46. {
  47. ViewCustomerStickRepository = viewCustomerStickRepository;
  48. SqlExecute = sqlExecute;
  49. CustomerRepository = customerRepository;
  50. OrderSendRepository = orderSendRepository;
  51. BusinessLogRepository = businessLogRepository;
  52. ViewStickBillRepository = viewStickBillRepository;
  53. ViewOrderSendStickBillRepository = viewOrderSendStickBillRepository;
  54. OrderSendBillRepository = orderSendBillRepository;
  55. ViewStatementBillRepository = viewStatementBillRepository;
  56. KeyIsAuto = false;
  57. QueryAppService = queryAppService;
  58. }
  59. protected override string GetPermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMg;
  60. protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMg;
  61. protected override string CreatePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceCreateCreate;
  62. protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMgUpdate;
  63. protected override string DeletePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMgDelete;
  64. public async Task<PagedResultDto<ViewStickBill>> GetViewAll(PagedRequestDto input)
  65. {
  66. CheckGetAllPermission();
  67. var query = ViewStickBillRepository.GetAll();
  68. if (input.SearchList != null && input.SearchList.Count > 0)
  69. {
  70. List<LambdaObject> objList = new List<LambdaObject>();
  71. foreach (var o in input.SearchList)
  72. {
  73. if (o.KeyWords.IsNullOrEmpty())
  74. continue;
  75. object keyWords = o.KeyWords;
  76. objList.Add(new LambdaObject
  77. {
  78. FieldType = (LambdaFieldType)o.FieldType,
  79. FieldName = o.KeyField,
  80. FieldValue = keyWords,
  81. ExpType = (LambdaExpType)o.ExpType
  82. });
  83. }
  84. var exp = objList.GetExp<ViewStickBill>();
  85. query = query.Where(exp);
  86. }
  87. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  88. query = query.OrderByDescending(i => i.CreatDate);
  89. query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
  90. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  91. var dtos = new PagedResultDto<ViewStickBill>(
  92. totalCount,
  93. entities
  94. );
  95. return dtos;
  96. }
  97. //[AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateQueryPage)]
  98. //public async Task<List<ViewCustomerStick>> GetAllCreateView(PagedRequestDto input)
  99. //{
  100. // var query = ViewCustomerStickRepository.GetAll();
  101. // if (input.SearchList != null && input.SearchList.Count > 0)
  102. // {
  103. // List<LambdaObject> objList = new List<LambdaObject>();
  104. // foreach (var o in input.SearchList)
  105. // {
  106. // if (o.KeyWords.IsNullOrEmpty())
  107. // continue;
  108. // object keyWords = o.KeyWords;
  109. // /*if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.LessOrEqual)
  110. // ]
  111. // keyWords = Convert.ToDateTime(keyWords).AddDays(1);
  112. // }*/
  113. // objList.Add(new LambdaObject
  114. // {
  115. // FieldType = (LambdaFieldType)o.FieldType,
  116. // FieldName = o.KeyField,
  117. // FieldValue = keyWords,
  118. // ExpType = (LambdaExpType)o.ExpType
  119. // });
  120. // }
  121. // var exp = objList.GetExp<ViewCustomerStick>();
  122. // query = query.Where(exp).OrderByDescending(i=>i.SendDate);
  123. // }
  124. // var entities = await AsyncQueryableExecuter.ToListAsync(query);
  125. // return entities;
  126. //}
  127. [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateQueryPage)]
  128. public async Task<List<ViewStatementBill>> GetAllCreateView(PagedRequestDto input)
  129. {
  130. var query = ViewStatementBillRepository.GetAll();
  131. if (input.SearchList != null && input.SearchList.Count > 0)
  132. {
  133. List<LambdaObject> objList = new List<LambdaObject>();
  134. foreach (var o in input.SearchList)
  135. {
  136. if (o.KeyWords.IsNullOrEmpty())
  137. continue;
  138. object keyWords = o.KeyWords;
  139. /*if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.LessOrEqual)
  140. ]
  141. keyWords = Convert.ToDateTime(keyWords).AddDays(1);
  142. }*/
  143. objList.Add(new LambdaObject
  144. {
  145. FieldType = (LambdaFieldType)o.FieldType,
  146. FieldName = o.KeyField,
  147. FieldValue = keyWords,
  148. ExpType = (LambdaExpType)o.ExpType
  149. });
  150. }
  151. var exp = objList.GetExp<ViewStatementBill>();
  152. query = query.Where(exp).OrderByDescending(i => i.CreationTime);
  153. }
  154. query = query.Where(i => i.OrderStickBillNo == null || i.OrderStickBillNo == "");
  155. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  156. return entities;
  157. }
  158. [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateCreate),AuditLog("创建发票")]
  159. public override async Task<OrderStickBillDto> Create(OrderStickBillCreateDto input)
  160. {
  161. //CheckCreatePermission();
  162. if (input.OrderSendIds.IsNullOrEmpty())
  163. {
  164. throw new UserFriendlyException("无效参数!");
  165. }
  166. var entity = Repository.FirstOrDefault(input.Id);
  167. if (entity != null)
  168. {
  169. throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!");
  170. }
  171. string sql = $"update StatementBill set StatementState=1,OrderStickBillNo='{input.StickNum}' where Id in ({input.OrderSendIds});";
  172. sql += $"update OrderSend set OrderStickBillNo='{input.StickNum}' where StatementBillNo in (select StatementBillNo from StatementBill where Id in ({input.OrderSendIds}));";
  173. //SqlExecute.Execute("update OrderSend set OrderStickBillNo='"+input.StickNum+"' where OrderSendId in ("+ input.OrderSendIds + ") ");
  174. SqlExecute.Execute(sql);
  175. //input.Amount = 0;
  176. input.InvoiceState = InvoiceState.NotPay.ToInt();
  177. input.InvoiceType = InvoiceTypeDefinition.Normal;
  178. return await CreateEntity(input);
  179. }
  180. [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgCreateRed),AuditLog("创建红冲发票")]
  181. public async Task<OrderStickBillDto> CreateRed(RedOrderStickBillCreateDto input)
  182. {
  183. var entity =await Repository.FirstOrDefaultAsync(input.Id);
  184. if (entity != null)
  185. {
  186. throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!");
  187. }
  188. var oi = await Repository.FirstOrDefaultAsync(a => a.StickNum == input.OriginalStickNum);
  189. if (oi == null)
  190. {
  191. throw new UserFriendlyException("原发票号不存在,请检查后再试!");
  192. }
  193. if ( input.InvoiceType != InvoiceTypeDefinition.RedLess && input.Amount> oi.Amount)
  194. {
  195. throw new UserFriendlyException($"退还金额不能大于原发票金额【{oi.Amount}】。");
  196. }
  197. entity = MapToEntity(input);
  198. entity.StickNum = entity.Id;
  199. entity.InvoiceState = InvoiceState.NotPay.ToInt();
  200. entity.CustomerId = oi.CustomerId;
  201. if (entity.InvoiceType== InvoiceTypeDefinition.RedOver || entity.InvoiceType== InvoiceTypeDefinition.RedReturn)
  202. {
  203. entity.Amount = entity.Amount > 0 ? 0 - entity.Amount : entity.Amount;
  204. }
  205. if (string.IsNullOrEmpty(entity.StickMan))
  206. {
  207. entity.StickMan = AbpSession.UserName;
  208. }
  209. //entity.InvoiceType = InvoiceTypeDefinition.RedReturn;
  210. entity.OriginalStickNum = input.OriginalStickNum;
  211. entity.OrderNo = input.OrderNo;
  212. entity.ReturnOrderNo = input.ReturnOrderNo;
  213. entity = await Repository.InsertAsync(entity);
  214. return MapToEntityDto(entity);
  215. }
  216. [AuditLog("作废发票")]
  217. public override async Task Delete(EntityDto<string> input)
  218. {
  219. CheckDeletePermission();
  220. var entity = Repository.Get(input.Id);
  221. var orderSends = OrderSendRepository.GetAllList(i => i.OrderStickBillNo == entity.Id);
  222. foreach (var send in orderSends)
  223. {
  224. send.OrderStickBillNo = "";
  225. send.TimeLastMod = Clock.Now;
  226. send.UserIDLastMod = AbpSession.UserName;
  227. await OrderSendRepository.UpdateAsync(send);
  228. }
  229. string sql = $"update StatementBill set StatementState=0,OrderStickBillNo='' where OrderStickBillNo ='{entity.Id}';";
  230. await SqlExecute.ExecuteAsync(sql);
  231. BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository, "撤销发票", $"发票{entity.ToJsonString()}撤销");
  232. //this.Logger.Info("撤销发票!" + entity.ToString());
  233. await Repository.DeleteAsync(input.Id);
  234. }
  235. [AuditLog("修改发票号")]
  236. public async Task<OrderStickBill> UpdateStickNum(OrderStickBillUpdateDto input)
  237. {
  238. CheckUpdatePermission();
  239. if (Repository.FirstOrDefault(i => i.StickNum == input.StickNum) != null)
  240. {
  241. throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!");
  242. }
  243. var entity = Repository.Get(input.Id);
  244. entity.StickNum = input.StickNum;
  245. entity.TimeLastMod = Clock.Now;
  246. entity.UserIDLastMod = AbpSession.UserName;
  247. this.Logger.Info("修改发票号!" + entity.ToString());
  248. return await Repository.UpdateAsync(entity);
  249. }
  250. [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgUpdateState), AuditLog("修改发票状态")]
  251. public async Task<OrderStickBill> UpdateState(OrderStickBillUpdateDto input)
  252. {
  253. var entity = Repository.Get(input.Id);
  254. entity.Amount = input.Amount;
  255. if (entity.InvoiceType== InvoiceTypeDefinition.RedOver || entity.InvoiceType== InvoiceTypeDefinition.RedReturn)
  256. {
  257. entity.Amount = entity.Amount > 0 ? 0 - entity.Amount : entity.Amount;
  258. }
  259. entity.InvoiceState = InvoiceState.HasPay.ToInt();
  260. entity.TimeLastMod = Clock.Now;
  261. entity.UserIDLastMod = AbpSession.UserName;
  262. this.Logger.Info("修改发票状态!" + entity.ToJsonString());
  263. return await Repository.UpdateAsync(entity);
  264. }
  265. [AuditLog("导出发票")]
  266. public async Task<string> ExportInvoice(EntityDto<string> input)
  267. {
  268. var orderSends =
  269. ViewOrderSendStickBillRepository.GetAll().Where(i => i.OrderStickBillNo == input.Id).OrderBy(i => i.SendDate).ToList();
  270. var bill = ObjectMapper.Map<OrderSendBillDto>(
  271. OrderSendBillRepository.Get(orderSends.First().OrderSendBillNo));
  272. var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId);
  273. string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/发票导出模板.xlsx";
  274. var savePath = "Download/Excel/InvoiceBill";
  275. var work = ExcelHelper.CreateWorkBook07(path);
  276. var sheet1 = work.GetSheet("Worksheet");
  277. sheet1.GenerateCell(4, 4).SetCellValue(customerInfo.CustomerName);
  278. sheet1.GenerateCell(4, 11).SetCellValue(bill.SendAddress);
  279. sheet1.GenerateCell(5, 4).SetCellValue(bill.ContactTels);
  280. sheet1.GenerateCell(5, 11).SetCellValue(bill.ContactMan);
  281. sheet1.GenerateCell(6, 4).SetCellValue(input.Id);
  282. sheet1.GenerateCell(6, 8).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日"));
  283. if (orderSends.Count>1)
  284. {
  285. sheet1.InsertRows(8, orderSends.Count-1);
  286. }
  287. int index = 0;
  288. decimal ldAccontTotal = 0;
  289. decimal ldNoTaxTotal = 0;
  290. string currencyId = "CNY";
  291. bool isCanViewPrice = PermissionChecker.IsGranted(PermissionNames.PagesOrderInfoOrderMgQueryOrderPrice);
  292. foreach (var send in orderSends)
  293. {
  294. var sendQuantity = $"{send.SendQuantity ?? 0:N3}";
  295. var price = $"{send.Price:N3}";
  296. var noTaxprice = $"{send.AfterTaxPrice:N3}";
  297. var logisticsFee = string.Format("{0:N3}", send.LogisticsFee);
  298. var moldFee = string.Format("{0:N3}", send.MoldFee);
  299. var totalprice = $"{send.totalprice:N3}";
  300. var totalNoTaxprice = $"{(send.SendQuantity ?? 0) * send.AfterTaxPrice:N3}";
  301. var sendDate = $"{send.SendDate:yyyy-MM-dd}";
  302. currencyId = send.CurrencyId;
  303. sheet1.GenerateCell(8 + index, 1).SetValue<int>(index + 1);
  304. sheet1.GenerateCell(8 + index, 2).SetValue(send.StockNo ?? send.OrderNo);
  305. sheet1.GenerateCell(8 + index, 3).SetValue(send.OrderSendBillNo ?? "");
  306. sheet1.GenerateCell(8 + index, 4).SetValue(sendDate);
  307. sheet1.GenerateCell(8 + index, 5).SetValue(send.PartNo ?? "");
  308. sheet1.GenerateCell(8 + index, 6).SetValue(send.Model ?? "");
  309. sheet1.GenerateCell(8 + index, 7).SetValue(send.ProductName ?? "");
  310. sheet1.GenerateCell(8 + index, 8).SetValue(send.Rigidity ?? "");
  311. sheet1.GenerateCell(8 + index, 9).SetValue(send.SurfaceColor ?? "");
  312. sheet1.GenerateCell(8 + index, 10).SetValue<decimal>(sendQuantity);
  313. sheet1.GenerateCell(8 + index, 11).SetValue(send.OrderUnitName??"");
  314. if (isCanViewPrice)
  315. {
  316. sheet1.GenerateCell(8 + index, 12).SetValue<decimal>(price);
  317. sheet1.GenerateCell(8 + index, 13).SetValue<decimal>(noTaxprice);
  318. sheet1.GenerateCell(8 + index, 14).SetValue<decimal>(logisticsFee);
  319. sheet1.GenerateCell(8 + index, 15).SetValue<decimal>(moldFee);
  320. sheet1.GenerateCell(8 + index, 16).SetValue<decimal>(totalprice);
  321. sheet1.GenerateCell(8 + index, 17).SetValue<decimal>(totalNoTaxprice);
  322. }
  323. ldAccontTotal += send.totalprice;
  324. ldNoTaxTotal += (send.SendQuantity ?? 0) * send.AfterTaxPrice;
  325. index++;
  326. }
  327. sheet1.GenerateCell(8 + index, 15).SetValue($"总金额({currencyId})");
  328. sheet1.GenerateCell(8 + index, 16).SetValue<decimal>(ldAccontTotal);
  329. sheet1.GenerateCell(8 + index, 17).SetValue<decimal>(ldNoTaxTotal);
  330. var fileName = $"发票单-{Clock.Now:yyMMddHHmmss}.xlsx";
  331. var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
  332. if (!result.IsNullOrEmpty())
  333. {
  334. //CheckErrors(IwbIdentityResult.Failed(result));
  335. return null;
  336. }
  337. return $"/{savePath}/{fileName}";
  338. }
  339. [AuditLog("导出发票"),AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgExportInvoices)]
  340. public async Task<string> ExportInvoices(PagedRequestDto input)
  341. {
  342. var query = ViewStickBillRepository.GetAll();
  343. if (input.SearchList != null && input.SearchList.Count > 0)
  344. {
  345. List<LambdaObject> objList = new List<LambdaObject>();
  346. foreach (var o in input.SearchList)
  347. {
  348. if (o.KeyWords.IsNullOrEmpty())
  349. continue;
  350. object keyWords = o.KeyWords;
  351. objList.Add(new LambdaObject
  352. {
  353. FieldType = (LambdaFieldType)o.FieldType,
  354. FieldName = o.KeyField,
  355. FieldValue = keyWords,
  356. ExpType = (LambdaExpType)o.ExpType
  357. });
  358. }
  359. var exp = objList.GetExp<ViewStickBill>();
  360. query = query.Where(exp);
  361. }
  362. var list =await query.OrderByDescending(i => i.CreatDate).ToListAsync();
  363. if (!list.Any())
  364. {
  365. CheckErrors(IwbIdentityResult.Failed("未查询到记录!"));
  366. return null;
  367. }
  368. string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/多发票导出模板.xlsx";
  369. var savePath = "Download/Excel/InvoiceBills";
  370. var work = ExcelHelper.CreateWorkBook07(path);
  371. var sheet = work.GetSheet("Worksheet");
  372. sheet.InsertRows(2,list.Count-1);
  373. var index = 2;
  374. foreach (var l in list)
  375. {
  376. sheet.GenerateCell(index,1).SetValue(l.Id);
  377. sheet.GenerateCell(index,2).SetValue(l.CustomerId);
  378. sheet.GenerateCell(index,3).SetValue(l.CustomerName);
  379. sheet.GenerateCell(index,4).SetValue<DateTime>(l.CreatDate??DateTime.MinValue);
  380. sheet.GenerateCell(index,5).SetValue(l.StickMan);
  381. sheet.GenerateCell(index,6).SetValue<decimal>(l.TotalPrice??0);
  382. sheet.GenerateCell(index,7).SetValue<decimal>(l.AfterTaxTotalPrice??0);
  383. sheet.GenerateCell(index,8).SetValue(l.InvoiceState==1?"未收款":"已收款");
  384. sheet.GenerateCell(index,9).SetValue<decimal>(l.Amount??0);
  385. sheet.GenerateCell(index,10).SetValue(l.CurrencyId);
  386. index++;
  387. }
  388. var fileName = $"发票单-{Clock.Now:yyMMddHHmmss}.xlsx";
  389. var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
  390. if (!result.IsNullOrEmpty())
  391. {
  392. //CheckErrors(IwbIdentityResult.Failed(result));
  393. return null;
  394. }
  395. return $"/{savePath}/{fileName}";
  396. }
  397. [AuditLog("导出对账单")]
  398. public async Task<string> ExportStatementBill(EntityDto<string> input)
  399. {
  400. var orderSends =
  401. ViewOrderSendStickBillRepository.GetAll().Where(i => i.StatementBillNo == input.Id).OrderBy(i=>i.SendDate).ThenBy(i => i.StatementBillSort).ToList();
  402. var bill = ObjectMapper.Map<OrderSendBillDto>(
  403. OrderSendBillRepository.Get(orderSends.First().OrderSendBillNo));
  404. var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId);
  405. var customerDefaultProducts = await QueryAppService.GetCustomerDefaultProductByCustomerId(customerInfo.Id);
  406. var productProperties = await QueryAppService.GetProductPropertyEntity(ProductProperty.ProductMaterial + "");
  407. string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/对账单导出模板.xlsx";
  408. var savePath = "Download/Excel/InvoiceBill";
  409. var work = ExcelHelper.CreateWorkBook07(path);
  410. var sheet1 = work.GetSheet("Worksheet");
  411. sheet1.GenerateCell(4, 4).SetCellValue(customerInfo.CustomerName);
  412. sheet1.GenerateCell(4, 12).SetCellValue(bill.SendAddress);
  413. sheet1.GenerateCell(5, 4).SetCellValue(bill.ContactTels);
  414. sheet1.GenerateCell(5, 12).SetCellValue(bill.ContactMan);
  415. sheet1.GenerateCell(6, 4).SetCellValue(input.Id);
  416. sheet1.GenerateCell(6, 9).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日"));
  417. if (orderSends.Count>1)
  418. {
  419. sheet1.InsertRows(8, orderSends.Count-1);
  420. }
  421. int index = 0;
  422. decimal ldAccontTotal = 0;
  423. decimal ldNoTaxTotal = 0;
  424. string currencyId = "CNY";
  425. bool isCanViewPrice = PermissionChecker.IsGranted(PermissionNames.PagesOrderInfoOrderMgQueryOrderPrice);
  426. foreach (var send in orderSends)
  427. {
  428. var sendQuantity = $"{send.SendQuantity ?? 0:N3}";
  429. var price = $"{send.Price:N3}";
  430. var noTaxprice = $"{send.AfterTaxPrice:N3}";
  431. var logisticsFee = string.Format("{0:N3}", send.LogisticsFee);
  432. var moldFee = string.Format("{0:N3}", send.MoldFee);
  433. var totalprice = $"{send.totalprice:N3}";
  434. var totalNoTaxprice = $"{(send.SendQuantity ?? 0) * send.AfterTaxPrice:N3}";
  435. var sendDate = $"{send.SendDate:yyyy-MM-dd}";
  436. currencyId = send.CurrencyId;
  437. string productHsCode =
  438. CommonQueryService.GetProductHsCode(send.CustomerId, send.ProductNo, customerDefaultProducts);
  439. string materialHsCode =
  440. CommonQueryService.GetMaterialHsCode(send.ProductNo, productProperties);
  441. sheet1.GenerateCell(8 + index, 1).SetValue<int>(index + 1);
  442. sheet1.GenerateCell(8 + index, 2).SetValue(send.StockNo ?? send.OrderNo);
  443. sheet1.GenerateCell(8 + index, 3).SetValue(send.OrderSendBillNo ?? "");
  444. sheet1.GenerateCell(8 + index, 4).SetValue(sendDate);
  445. sheet1.GenerateCell(8 + index, 5).SetValue(send.PartNo ?? "");
  446. sheet1.GenerateCell(8 + index, 6).SetValue(productHsCode);
  447. sheet1.GenerateCell(8 + index, 7).SetValue(send.Model ?? "");
  448. sheet1.GenerateCell(8 + index, 8).SetValue(send.Material ?? "");
  449. sheet1.GenerateCell(8 + index, 9).SetValue(materialHsCode);
  450. sheet1.GenerateCell(8 + index, 10).SetValue(send.ProductName ?? "");
  451. sheet1.GenerateCell(8 + index, 11).SetValue(send.Rigidity ?? "");
  452. sheet1.GenerateCell(8 + index, 12).SetValue(send.SurfaceColor ?? "");
  453. sheet1.GenerateCell(8 + index, 13).SetValue<decimal>(sendQuantity);
  454. sheet1.GenerateCell(8 + index, 14).SetValue(send.OrderUnitName??"");
  455. if (isCanViewPrice)
  456. {
  457. sheet1.GenerateCell(8 + index, 15).SetValue<decimal>(price);
  458. sheet1.GenerateCell(8 + index, 16).SetValue<decimal>(noTaxprice);
  459. sheet1.GenerateCell(8 + index, 17).SetValue<decimal>(logisticsFee);
  460. sheet1.GenerateCell(8 + index, 18).SetValue<decimal>(moldFee);
  461. sheet1.GenerateCell(8 + index, 19).SetValue<decimal>(totalprice);
  462. sheet1.GenerateCell(8 + index, 20).SetValue<decimal>(totalNoTaxprice);
  463. }
  464. ldAccontTotal += send.totalprice;
  465. ldNoTaxTotal += (send.SendQuantity ?? 0) * send.AfterTaxPrice;
  466. index++;
  467. }
  468. sheet1.GenerateCell(8 + index, 18).SetValue($"总金额({currencyId})");
  469. sheet1.GenerateCell(8 + index, 19).SetValue<decimal>(ldAccontTotal);
  470. sheet1.GenerateCell(8 + index, 20).SetValue<decimal>(ldNoTaxTotal);
  471. var fileName = $"对账单-{Clock.Now:yyMMddHHmmss}.xlsx";
  472. var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
  473. if (!result.IsNullOrEmpty())
  474. {
  475. //CheckErrors(IwbIdentityResult.Failed(result));
  476. return null;
  477. }
  478. return $"/{savePath}/{fileName}";
  479. }
  480. }
  481. }