using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.SqlClient; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using Abp.Application.Services.Dto; using Abp.Authorization; using Abp.Collections.Extensions; using Abp.Domain.Repositories; using Abp.Extensions; using Abp.Json; using Abp.Timing; using Abp.UI; using IwbZero.AppServiceBase; using IwbZero.Auditing; using IwbZero.IdentityFramework; using Microsoft.AspNet.SignalR.Hubs; using ShwasherSys.Authorization.Permissions; using ShwasherSys.BaseSysInfo; using ShwasherSys.Common; using ShwasherSys.CustomerInfo; using ShwasherSys.EntityFramework; using ShwasherSys.Invoice.Dto; using ShwasherSys.Lambda; using ShwasherSys.OrderSendInfo; using ShwasherSys.OrderSendInfo.Dto; namespace ShwasherSys.Invoice { [AbpAuthorize] public class OrderStickBillAppService : ShwasherAsyncCrudAppService, IOrderStickBillAppService { protected IRepository ViewCustomerStickRepository; protected IRepository ViewOrderSendStickBillRepository; public IRepository OrderSendBillRepository; protected IRepository CustomerRepository; protected IRepository OrderSendRepository; protected ISqlExecuter SqlExecute; protected IRepository BusinessLogRepository { get; } protected IRepository ViewStickBillRepository { get; } protected IRepository ViewStatementBillRepository { get; } public OrderStickBillAppService(IRepository repository, IRepository viewCustomerStickRepository, ISqlExecuter sqlExecute, IRepository customerRepository, IRepository orderSendRepository, IRepository businessLogRepository, IRepository viewStickBillRepository,IRepository viewOrderSendStickBillRepository, IRepository orderSendBillRepository, IRepository viewStatementBillRepository) : base(repository) { ViewCustomerStickRepository = viewCustomerStickRepository; SqlExecute = sqlExecute; CustomerRepository = customerRepository; OrderSendRepository = orderSendRepository; BusinessLogRepository = businessLogRepository; ViewStickBillRepository = viewStickBillRepository; ViewOrderSendStickBillRepository = viewOrderSendStickBillRepository; OrderSendBillRepository = orderSendBillRepository; ViewStatementBillRepository = viewStatementBillRepository; KeyIsAuto = false; } protected override string GetPermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMg; protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMg; protected override string CreatePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceCreateCreate; protected override string UpdatePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMgUpdate; protected override string DeletePermissionName { get; set; } = PermissionNames.PagesInvoiceInfoInvoiceMgDelete; public async Task> GetViewAll(PagedRequestDto input) { CheckGetAllPermission(); var query = ViewStickBillRepository.GetAll(); if (input.SearchList != null && input.SearchList.Count > 0) { List objList = new List(); foreach (var o in input.SearchList) { if (o.KeyWords.IsNullOrEmpty()) continue; object keyWords = o.KeyWords; objList.Add(new LambdaObject { FieldType = (LambdaFieldType)o.FieldType, FieldName = o.KeyField, FieldValue = keyWords, ExpType = (LambdaExpType)o.ExpType }); } var exp = objList.GetExp(); query = query.Where(exp); } var totalCount = await AsyncQueryableExecuter.CountAsync(query); query = query.OrderByDescending(i => i.CreatDate); query = query.Skip(input.SkipCount).Take(input.MaxResultCount); var entities = await AsyncQueryableExecuter.ToListAsync(query); var dtos = new PagedResultDto( totalCount, entities ); return dtos; } //[AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateQueryPage)] //public async Task> GetAllCreateView(PagedRequestDto input) //{ // var query = ViewCustomerStickRepository.GetAll(); // if (input.SearchList != null && input.SearchList.Count > 0) // { // List objList = new List(); // foreach (var o in input.SearchList) // { // if (o.KeyWords.IsNullOrEmpty()) // continue; // object keyWords = o.KeyWords; // /*if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.LessOrEqual) // ] // keyWords = Convert.ToDateTime(keyWords).AddDays(1); // }*/ // objList.Add(new LambdaObject // { // FieldType = (LambdaFieldType)o.FieldType, // FieldName = o.KeyField, // FieldValue = keyWords, // ExpType = (LambdaExpType)o.ExpType // }); // } // var exp = objList.GetExp(); // query = query.Where(exp).OrderByDescending(i=>i.SendDate); // } // var entities = await AsyncQueryableExecuter.ToListAsync(query); // return entities; //} [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateQueryPage)] public async Task> GetAllCreateView(PagedRequestDto input) { var query = ViewStatementBillRepository.GetAll(); if (input.SearchList != null && input.SearchList.Count > 0) { List objList = new List(); foreach (var o in input.SearchList) { if (o.KeyWords.IsNullOrEmpty()) continue; object keyWords = o.KeyWords; /*if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.LessOrEqual) ] keyWords = Convert.ToDateTime(keyWords).AddDays(1); }*/ objList.Add(new LambdaObject { FieldType = (LambdaFieldType)o.FieldType, FieldName = o.KeyField, FieldValue = keyWords, ExpType = (LambdaExpType)o.ExpType }); } var exp = objList.GetExp(); query = query.Where(exp).OrderByDescending(i => i.CreationTime); } query = query.Where(i => i.OrderStickBillNo == null || i.OrderStickBillNo == ""); var entities = await AsyncQueryableExecuter.ToListAsync(query); return entities; } [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceCreateCreate),AuditLog("创建发票")] public override async Task Create(OrderStickBillCreateDto input) { //CheckCreatePermission(); if (input.OrderSendIds.IsNullOrEmpty()) { throw new UserFriendlyException("无效参数!"); } var entity = Repository.FirstOrDefault(input.Id); if (entity != null) { throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!"); } string sql = $"update StatementBill set StatementState=1,OrderStickBillNo='{input.StickNum}' where Id in ({input.OrderSendIds});"; sql += $"update OrderSend set OrderStickBillNo='{input.StickNum}' where StatementBillNo in (select StatementBillNo from StatementBill where Id in ({input.OrderSendIds}));"; //SqlExecute.Execute("update OrderSend set OrderStickBillNo='"+input.StickNum+"' where OrderSendId in ("+ input.OrderSendIds + ") "); SqlExecute.Execute(sql); //input.Amount = 0; input.InvoiceState = InvoiceState.NotPay.ToInt(); input.InvoiceType = InvoiceTypeDefinition.Normal; return await CreateEntity(input); } [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgCreateRed),AuditLog("创建红冲发票")] public async Task CreateRed(RedOrderStickBillCreateDto input) { var entity =await Repository.FirstOrDefaultAsync(input.Id); if (entity != null) { throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!"); } var oi = await Repository.FirstOrDefaultAsync(a => a.StickNum == input.OriginalStickNum); if (oi == null) { throw new UserFriendlyException("原发票号不存在,请检查后再试!"); } if ( input.InvoiceType != InvoiceTypeDefinition.RedLess && input.Amount> oi.Amount) { throw new UserFriendlyException($"退还金额不能大于原发票金额【{oi.Amount}】。"); } entity = MapToEntity(input); entity.StickNum = entity.Id; entity.InvoiceState = InvoiceState.NotPay.ToInt(); entity.CustomerId = oi.CustomerId; if (entity.InvoiceType== InvoiceTypeDefinition.RedOver || entity.InvoiceType== InvoiceTypeDefinition.RedReturn) { entity.Amount = entity.Amount > 0 ? 0 - entity.Amount : entity.Amount; } if (string.IsNullOrEmpty(entity.StickMan)) { entity.StickMan = AbpSession.UserName; } //entity.InvoiceType = InvoiceTypeDefinition.RedReturn; entity.OriginalStickNum = input.OriginalStickNum; entity.OrderNo = input.OrderNo; entity.ReturnOrderNo = input.ReturnOrderNo; entity = await Repository.InsertAsync(entity); return MapToEntityDto(entity); } [AuditLog("作废发票")] public override async Task Delete(EntityDto input) { CheckDeletePermission(); var entity = Repository.Get(input.Id); var orderSends = OrderSendRepository.GetAllList(i => i.OrderStickBillNo == entity.Id); foreach (var send in orderSends) { send.OrderStickBillNo = ""; send.TimeLastMod = Clock.Now; send.UserIDLastMod = AbpSession.UserName; await OrderSendRepository.UpdateAsync(send); } string sql = $"update StatementBill set StatementState=0,OrderStickBillNo='' where OrderStickBillNo ='{entity.Id}';"; await SqlExecute.ExecuteAsync(sql); BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository, "撤销发票", $"发票{entity.ToJsonString()}撤销"); //this.Logger.Info("撤销发票!" + entity.ToString()); await Repository.DeleteAsync(input.Id); } [AuditLog("修改发票号")] public async Task UpdateStickNum(OrderStickBillUpdateDto input) { CheckUpdatePermission(); if (Repository.FirstOrDefault(i => i.StickNum == input.StickNum) != null) { throw new UserFriendlyException("该发票号已存在,请勿重复创建相同发票号!"); } var entity = Repository.Get(input.Id); entity.StickNum = input.StickNum; entity.TimeLastMod = Clock.Now; entity.UserIDLastMod = AbpSession.UserName; this.Logger.Info("修改发票号!" + entity.ToString()); return await Repository.UpdateAsync(entity); } [AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgUpdateState), AuditLog("修改发票状态")] public async Task UpdateState(OrderStickBillUpdateDto input) { var entity = Repository.Get(input.Id); entity.Amount = input.Amount; if (entity.InvoiceType== InvoiceTypeDefinition.RedOver || entity.InvoiceType== InvoiceTypeDefinition.RedReturn) { entity.Amount = entity.Amount > 0 ? 0 - entity.Amount : entity.Amount; } entity.InvoiceState = InvoiceState.HasPay.ToInt(); entity.TimeLastMod = Clock.Now; entity.UserIDLastMod = AbpSession.UserName; this.Logger.Info("修改发票状态!" + entity.ToJsonString()); return await Repository.UpdateAsync(entity); } [AuditLog("导出发票")] public async Task ExportInvoice(EntityDto input) { var orderSends = ViewOrderSendStickBillRepository.GetAll().Where(i => i.OrderStickBillNo == input.Id).OrderBy(i => i.SendDate).ToList(); var bill = ObjectMapper.Map( OrderSendBillRepository.Get(orderSends.First().OrderSendBillNo)); var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId); string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/发票导出模板.xlsx"; var savePath = "Download/Excel/InvoiceBill"; var work = ExcelHelper.CreateWorkBook07(path); var sheet1 = work.GetSheet("Worksheet"); sheet1.GenerateCell(4, 4).SetCellValue(customerInfo.CustomerName); sheet1.GenerateCell(4, 11).SetCellValue(bill.SendAddress); sheet1.GenerateCell(5, 4).SetCellValue(bill.ContactTels); sheet1.GenerateCell(5, 11).SetCellValue(bill.ContactMan); sheet1.GenerateCell(6, 4).SetCellValue(input.Id); sheet1.GenerateCell(6, 8).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日")); if (orderSends.Count>1) { sheet1.InsertRows(8, orderSends.Count-1); } int index = 0; decimal ldAccontTotal = 0; decimal ldNoTaxTotal = 0; string currencyId = "CNY"; bool isCanViewPrice = PermissionChecker.IsGranted(PermissionNames.PagesOrderInfoOrderMgQueryOrderPrice); foreach (var send in orderSends) { var sendQuantity = $"{send.SendQuantity ?? 0:N3}"; var price = $"{send.Price:N3}"; var noTaxprice = $"{send.AfterTaxPrice:N3}"; var logisticsFee = string.Format("{0:N3}", send.LogisticsFee); var moldFee = string.Format("{0:N3}", send.MoldFee); var totalprice = $"{send.totalprice:N3}"; var totalNoTaxprice = $"{(send.SendQuantity ?? 0) * send.AfterTaxPrice:N3}"; var sendDate = $"{send.SendDate:yyyy-MM-dd}"; currencyId = send.CurrencyId; sheet1.GenerateCell(8 + index, 1).SetValue(index + 1); sheet1.GenerateCell(8 + index, 2).SetValue(send.StockNo ?? send.OrderNo); sheet1.GenerateCell(8 + index, 3).SetValue(send.OrderSendBillNo ?? ""); sheet1.GenerateCell(8 + index, 4).SetValue(sendDate); sheet1.GenerateCell(8 + index, 5).SetValue(send.PartNo ?? ""); sheet1.GenerateCell(8 + index, 6).SetValue(send.Model ?? ""); sheet1.GenerateCell(8 + index, 7).SetValue(send.ProductName ?? ""); sheet1.GenerateCell(8 + index, 8).SetValue(send.Rigidity ?? ""); sheet1.GenerateCell(8 + index, 9).SetValue(send.SurfaceColor ?? ""); sheet1.GenerateCell(8 + index, 10).SetValue(sendQuantity); sheet1.GenerateCell(8 + index, 11).SetValue(send.OrderUnitName??""); if (isCanViewPrice) { sheet1.GenerateCell(8 + index, 12).SetValue(price); sheet1.GenerateCell(8 + index, 13).SetValue(noTaxprice); sheet1.GenerateCell(8 + index, 14).SetValue(logisticsFee); sheet1.GenerateCell(8 + index, 15).SetValue(moldFee); sheet1.GenerateCell(8 + index, 16).SetValue(totalprice); sheet1.GenerateCell(8 + index, 17).SetValue(totalNoTaxprice); } ldAccontTotal += send.totalprice; ldNoTaxTotal += (send.SendQuantity ?? 0) * send.AfterTaxPrice; index++; } sheet1.GenerateCell(8 + index, 15).SetValue($"总金额({currencyId})"); sheet1.GenerateCell(8 + index, 16).SetValue(ldAccontTotal); sheet1.GenerateCell(8 + index, 17).SetValue(ldNoTaxTotal); var fileName = $"发票单-{Clock.Now:yyMMddHHmmss}.xlsx"; var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName); if (!result.IsNullOrEmpty()) { //CheckErrors(IwbIdentityResult.Failed(result)); return null; } return $"/{savePath}/{fileName}"; } [AuditLog("导出发票"),AbpAuthorize(PermissionNames.PagesInvoiceInfoInvoiceMgExportInvoices)] public async Task ExportInvoices(PagedRequestDto input) { var query = ViewStickBillRepository.GetAll(); if (input.SearchList != null && input.SearchList.Count > 0) { List objList = new List(); foreach (var o in input.SearchList) { if (o.KeyWords.IsNullOrEmpty()) continue; object keyWords = o.KeyWords; objList.Add(new LambdaObject { FieldType = (LambdaFieldType)o.FieldType, FieldName = o.KeyField, FieldValue = keyWords, ExpType = (LambdaExpType)o.ExpType }); } var exp = objList.GetExp(); query = query.Where(exp); } var list =await query.OrderByDescending(i => i.CreatDate).ToListAsync(); if (!list.Any()) { CheckErrors(IwbIdentityResult.Failed("未查询到记录!")); return null; } string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/多发票导出模板.xlsx"; var savePath = "Download/Excel/InvoiceBills"; var work = ExcelHelper.CreateWorkBook07(path); var sheet = work.GetSheet("Worksheet"); sheet.InsertRows(2,list.Count-1); var index = 2; foreach (var l in list) { sheet.GenerateCell(index,1).SetValue(l.Id); sheet.GenerateCell(index,2).SetValue(l.CustomerId); sheet.GenerateCell(index,3).SetValue(l.CustomerName); sheet.GenerateCell(index,4).SetValue(l.CreatDate??DateTime.MinValue); sheet.GenerateCell(index,5).SetValue(l.StickMan); sheet.GenerateCell(index,6).SetValue(l.TotalPrice??0); sheet.GenerateCell(index,7).SetValue(l.AfterTaxTotalPrice??0); sheet.GenerateCell(index,8).SetValue(l.InvoiceState==1?"未收款":"已收款"); sheet.GenerateCell(index,9).SetValue(l.Amount??0); sheet.GenerateCell(index,10).SetValue(l.CurrencyId); index++; } var fileName = $"发票单-{Clock.Now:yyMMddHHmmss}.xlsx"; var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName); if (!result.IsNullOrEmpty()) { //CheckErrors(IwbIdentityResult.Failed(result)); return null; } return $"/{savePath}/{fileName}"; } [AuditLog("导出对账单")] public async Task ExportStatementBill(EntityDto input) { var orderSends = ViewOrderSendStickBillRepository.GetAll().Where(i => i.StatementBillNo == input.Id).OrderBy(i=>i.SendDate).ThenBy(i => i.StatementBillSort).ToList(); var bill = ObjectMapper.Map( OrderSendBillRepository.Get(orderSends.First().OrderSendBillNo)); var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId); string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/InvoiceTemplate/对账单导出模板.xlsx"; var savePath = "Download/Excel/InvoiceBill"; var work = ExcelHelper.CreateWorkBook07(path); var sheet1 = work.GetSheet("Worksheet"); sheet1.GenerateCell(4, 4).SetCellValue(customerInfo.CustomerName); sheet1.GenerateCell(4, 12).SetCellValue(bill.SendAddress); sheet1.GenerateCell(5, 4).SetCellValue(bill.ContactTels); sheet1.GenerateCell(5, 12).SetCellValue(bill.ContactMan); sheet1.GenerateCell(6, 4).SetCellValue(input.Id); sheet1.GenerateCell(6, 9).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日")); if (orderSends.Count>1) { sheet1.InsertRows(8, orderSends.Count-1); } int index = 0; decimal ldAccontTotal = 0; decimal ldNoTaxTotal = 0; string currencyId = "CNY"; bool isCanViewPrice = PermissionChecker.IsGranted(PermissionNames.PagesOrderInfoOrderMgQueryOrderPrice); foreach (var send in orderSends) { var sendQuantity = $"{send.SendQuantity ?? 0:N3}"; var price = $"{send.Price:N3}"; var noTaxprice = $"{send.AfterTaxPrice:N3}"; var logisticsFee = string.Format("{0:N3}", send.LogisticsFee); var moldFee = string.Format("{0:N3}", send.MoldFee); var totalprice = $"{send.totalprice:N3}"; var totalNoTaxprice = $"{(send.SendQuantity ?? 0) * send.AfterTaxPrice:N3}"; var sendDate = $"{send.SendDate:yyyy-MM-dd}"; currencyId = send.CurrencyId; sheet1.GenerateCell(8 + index, 1).SetValue(index + 1); sheet1.GenerateCell(8 + index, 2).SetValue(send.StockNo ?? send.OrderNo); sheet1.GenerateCell(8 + index, 3).SetValue(send.OrderSendBillNo ?? ""); sheet1.GenerateCell(8 + index, 4).SetValue(sendDate); sheet1.GenerateCell(8 + index, 5).SetValue(send.PartNo ?? ""); sheet1.GenerateCell(8 + index, 6).SetValue(send.Model ?? ""); sheet1.GenerateCell(8 + index, 7).SetValue(send.Material ?? ""); sheet1.GenerateCell(8 + index, 8).SetValue(send.ProductName ?? ""); sheet1.GenerateCell(8 + index, 9).SetValue(send.Rigidity ?? ""); sheet1.GenerateCell(8 + index, 10).SetValue(send.SurfaceColor ?? ""); sheet1.GenerateCell(8 + index, 11).SetValue(sendQuantity); sheet1.GenerateCell(8 + index, 12).SetValue(send.OrderUnitName??""); if (isCanViewPrice) { sheet1.GenerateCell(8 + index, 13).SetValue(price); sheet1.GenerateCell(8 + index, 14).SetValue(noTaxprice); sheet1.GenerateCell(8 + index, 15).SetValue(logisticsFee); sheet1.GenerateCell(8 + index, 16).SetValue(moldFee); sheet1.GenerateCell(8 + index, 17).SetValue(totalprice); sheet1.GenerateCell(8 + index, 18).SetValue(totalNoTaxprice); } ldAccontTotal += send.totalprice; ldNoTaxTotal += (send.SendQuantity ?? 0) * send.AfterTaxPrice; index++; } sheet1.GenerateCell(8 + index, 16).SetValue($"总金额({currencyId})"); sheet1.GenerateCell(8 + index, 17).SetValue(ldAccontTotal); sheet1.GenerateCell(8 + index, 18).SetValue(ldNoTaxTotal); var fileName = $"对账单-{Clock.Now:yyMMddHHmmss}.xlsx"; var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName); if (!result.IsNullOrEmpty()) { //CheckErrors(IwbIdentityResult.Failed(result)); return null; } return $"/{savePath}/{fileName}"; } } }