| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Web.Mvc;
- using Abp.Application.Services.Dto;
- using Abp.Authorization;
- using Abp.Domain.Repositories;
- using Abp.Extensions;
- using Abp.Json;
- using Abp.Timing;
- using Castle.MicroKernel.Registration.Interceptor;
- using IwbZero.AppServiceBase;
- using IwbZero.Auditing;
- using IwbZero.IdentityFramework;
- using Microsoft.AspNet.Identity;
- using MiniExcelLibs;
- using NPOI.SS.UserModel;
- using ShwasherSys.Authorization.Permissions;
- using ShwasherSys.BaseSysInfo;
- using ShwasherSys.Common;
- using ShwasherSys.Common.Dto;
- using ShwasherSys.CustomerInfo;
- using ShwasherSys.EntityFramework;
- using ShwasherSys.Lambda;
- using ShwasherSys.Order;
- using ShwasherSys.OrderSendInfo.Dto;
- using ShwasherSys.ProductStoreInfo;
- namespace ShwasherSys.OrderSendInfo
- {
- [AbpAuthorize]
- public class OrderSendBillAppService : ShwasherAsyncCrudAppService<OrderSendBill, OrderSendBillDto, string, PagedRequestDto, OrderSendBillCreateDto, OrderSendBillUpdateDto >, IOrderSendBillAppService
- {
- protected IRepository<OrderSend> OrderSendRepository;
- protected IRepository<OrderItem> OrderItemRepository;
- protected IRepository<ViewOrderSend> ViewOrderSendRepository;
- protected IRepository<ViewOrderSendBill,string> ViewOrderSendBillRepository;
- protected IRepository<FinshedEnterStore> FinshedEnterStoreRepository;
- protected IRepository<CurrentProductStoreHouse> CurrentProductStoreHouseRepository;
- protected IRepository<ProductOutStore> ProductOutStoreRepository;
- protected IQueryAppService QueryAppService { get; }
- protected ICommonAppService CommonAppService { get; }
- protected IRepository<BusinessLog> BusinessLogRepository { get; }
- protected IRepository<Customer,string> CustomerRepository { get; }
- protected ISqlExecuter SqlExecuter;
- public OrderSendBillAppService(IRepository<OrderSendBill, string> repository, IRepository<OrderSend> orderSendRepository, ISqlExecuter sqlExecuter, IRepository<OrderItem> orderItemRepository, IRepository<ViewOrderSend> viewOrderSendRepository, IRepository<ViewOrderSendBill,string> viewOrderSendBillRepository, IRepository<FinshedEnterStore> finshedEnterStoreRepository, IRepository<CurrentProductStoreHouse> currentProductStoreHouseRepository, IRepository<ProductOutStore> productOutStoreRepository, IRepository<BusinessLog> businessLogRepository, IRepository<Customer, string> customerRepository, IQueryAppService queryAppService, ICommonAppService commonAppService) : base(repository)
- {
- OrderSendRepository = orderSendRepository;
- SqlExecuter = sqlExecuter;
- OrderItemRepository = orderItemRepository;
- ViewOrderSendRepository = viewOrderSendRepository;
- ViewOrderSendBillRepository = viewOrderSendBillRepository;
- FinshedEnterStoreRepository = finshedEnterStoreRepository;
- CurrentProductStoreHouseRepository = currentProductStoreHouseRepository;
- ProductOutStoreRepository = productOutStoreRepository;
- BusinessLogRepository = businessLogRepository;
- CustomerRepository = customerRepository;
- QueryAppService = queryAppService;
- CommonAppService = commonAppService;
- }
- protected override string GetPermissionName { get; set; } = PermissionNames.PagesSendGoodsOrderSendBillCreate;
- protected override string GetAllPermissionName { get; set; } = PermissionNames.PagesSendGoodsOrderSendBillCreate;
- protected override string CreatePermissionName { get; set; } = PermissionNames.PagesSendGoodsOrderSendBillCreateCreate;
- protected override string DeletePermissionName { get; set; } = PermissionNames.PagesSendGoodsOrderSendBillCreateDelete;
- #region 发货管理
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillCreateDelete),AuditLog("撤销发货")]
- public bool CancelOrderSend(EntityDto<string> input)
- {
- bool lbRetval = false;
- string orderSendIds = input.Id;
- try
- {
- var arrOrderSendIds = orderSendIds.Split(',');
- foreach (var orderSendId in arrOrderSendIds)
- {
- OrderSend loSendInfo = OrderSendRepository.Get(Convert.ToInt32(orderSendId));
- OrderItem loOrderItem = OrderItemRepository.Get(loSendInfo.OrderItemId);
- //-------------- 添加订单发货撤销后入库申请逻辑 ---------------
- var productOutStore = ProductOutStoreRepository.FirstOrDefault(i => i.OrderSendId == loSendInfo.Id);
- if (productOutStore == null)
- {
- CheckErrors(IwbIdentityResult.Failed("未发现出库记录!"));
- return false;
- }
- if (productOutStore.ApplyStatus==FinshedOutStoreApplyStatusEnum.OutStored.ToInt())
- {
- CheckErrors(IwbIdentityResult.Failed("发货记录:"+loSendInfo.Id+"的出库申请已确认出库,发货明细不可撤销!"));
- return false;
- }
- productOutStore.IsClose = true;//关闭出库申请记录
- ProductOutStoreRepository.UpdateAsync(productOutStore);
- CurrentProductStoreHouse currentStore = CurrentProductStoreHouseRepository.FirstOrDefault(i =>
- i.CurrentProductStoreHouseNo == loSendInfo.CurrentProductStoreHouseNo);
- if (currentStore == null)
- {
- CheckErrors(IwbIdentityResult.Failed("未发现库存!"));
- return false;
- }
- currentStore.FreezeQuantity -= loSendInfo.SendQuantity;//恢复冻结库存
- currentStore.TimeLastMod = Clock.Now;
- CurrentProductStoreHouseRepository.UpdateAsync(currentStore);
- loOrderItem.OrderItemStatusId = OrderItemStatusEnum.Audited.ToInt();//更新订单明细状态
- OrderSendRepository.DeleteAsync(loSendInfo);//删除发货明细
- loOrderItem.TimeLastMod = Clock.Now;
- loOrderItem.UserIDLastMod = AbpSession.UserName;
- OrderItemRepository.UpdateAsync(loOrderItem);
- BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository,"撤销发货",$"发货单{loSendInfo.ToJsonString()}取消发货,库存编码为:{currentStore.CurrentProductStoreHouseNo}的库存冻结数量减少{loSendInfo.SendQuantity}!订单明细状态变更为发货中..{loOrderItem.ToJsonString()}");
- }
- this.LogInfo("撤销发货明细:" + input.Id + " 成功!");
- lbRetval = true;
- }
- catch (Exception e)
- {
- this.LogError("撤销发货明细:" + input.Id + " 失败!" + e);
- throw;
- }
- return lbRetval;
- }
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillCreate),AuditLog("根据客户编号,查询出待生成发货单的发货明细")]
- public async Task<List<ViewOrderSend>> GetOrderSendByCustomerId(QuerySendDto input)
- {
- var queryAllList = ViewOrderSendRepository.GetAll().Where(i =>
- i.CustomerId == input.CustomerId && (i.OrderSendBillNo == null || i.OrderSendBillNo == "") &&
- i.CustomerSendId == input.CustomerSendId).OrderByDescending(i=>i.SendDate);
- var query = await AsyncQueryableExecuter.ToListAsync(queryAllList);
- if (input.SendDateStart != null)
- {
- query= query.Where(i => i.SendDate >= input.SendDateStart).ToList();
- }
- if (input.SendDateEnd != null)
- {
- query = query.Where(i => i.SendDate < input.SendDateEnd).ToList();
- }
- return query;
- }
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillCreate),AuditLog("查询出待生成发货单的客户列表记录")]
- public List<SelectListItem> GetHasSendOrderCustomer()
- {
- List<SelectListItem> listItems = new List<SelectListItem>();
- var queryAllList = ViewOrderSendRepository.GetAll().Where(i => (i.OrderSendBillNo == null || i.OrderSendBillNo == "") && i.CustomerId != null).Select(i => i.CustomerId).Distinct();
- foreach (var send in queryAllList)
- {
- listItems.Add(new SelectListItem()
- {
- Text = send,
- Value = send
- });
- }
- return listItems;
- }
- #endregion
- #region 发货单管理
- /// <summary>
- /// 生成发货单号
- /// </summary>
- /// <returns></returns>
- public string GetNewOrderSendBillNo()
- {
- string lcRetVal = "";
- DateTime loTiem = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-01");
- loTiem = loTiem.AddSeconds(-1);
- var bills = Repository.GetAll().Where(i => i.TimeCreated > loTiem).OrderByDescending(i => i.Id);
- if (bills.Any())
- {
- int liTempNo = 0;
- string lcTempNo = bills.First().Id;
- lcTempNo = lcTempNo.Remove(0, 1);
- liTempNo = Convert.ToInt32(lcTempNo);
- liTempNo++;
- lcRetVal = "B" + liTempNo;
- if (lcRetVal.Length < 10)
- {
- lcRetVal = "0" + lcRetVal;
- }
- }
- else
- {
- DateTime loDate = DateTime.Today;
- string month = loDate.Date.Month < 10 ? "0" + loDate.Date.Month : loDate.Date.Month.ToString();
- lcRetVal = "B" + loDate.Date.Year + month + "0001";
- }
- return lcRetVal;
- }
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillCreateCreate),AuditLog("发货单生成")]
- public override async Task<OrderSendBillDto> Create(OrderSendBillCreateDto input)
- {
- string orderSendIds = input.OrderSendIds;
- input.Id = GetNewOrderSendBillNo();
- input.SendDate = Clock.Now;
- input.TimeCreated = input.SendDate;
- input.TimeLastMod = input.SendDate;
- input.UserIDLastMod = AbpSession.UserName;
- input.IsDoBill = "N";
- //string lcSql = "update OrderSend set OrderSendBillNo='" + input.Id + "' ";
- //lcSql += " where OrderSendId in(" + orderSendIds + ")";
- //var sCount = SqlExecuter.Execute(lcSql);
- string lcSql = "";
- var osi = Array.ConvertAll<string, int>(orderSendIds.Split(','), int.Parse);
- for (int i = 0; i < osi.Length; i++)
- {
- lcSql += $"update OrderSend set OrderSendBillNo='{input.Id}',SendBillSort=${i+1} where OrderSendId = ${osi[i]};\r\n ";
- }
- var sCount = SqlExecuter.Execute(lcSql);
- //var oss =await OrderSendRepository.GetAllListAsync(i => osi.Contains(i.Id));
- //for (int i =0; i < oss.Count; i++)
- //{
- // oss[i].SendBillSort = i + 1;
- // await OrderSendRepository.UpdateAsync(oss[i]);
- //}
- var dto = await CreateEntity(input);
- var customer = await CustomerRepository.FirstOrDefaultAsync(a => a.Id == dto.CustomerId);
- if (string.IsNullOrEmpty(customer.Email))
- {
- return dto;
- }
- var orders = await ViewOrderSendRepository.GetAllListAsync(a => (orderSendIds + ",").Contains(a.Id + ","));
- if (orders.Any())
- {
- var expressName =await QueryAppService.GetExpressNameById(input.ExpressId ?? 0);
- string msg = $"发货快递:{expressName },快递单号:{input.ExpressBillNo}\r\n\r\n";
- foreach (var os in orders)
- {
- msg += $"订单号:{os.StockNo},产品名称:{os.ProductName},发货数量:{os.SendQuantity}\r\n";
- }
- CommonAppService.SendEmail(customer.Email,"您有订单已发货",msg,false);
- }
- return dto;
- }
- private (int, int) GetStatementCount(IQueryable<OrderSend> query,string orderSendBillNo)
- {
- var all = query.Where(i => i.OrderSendBillNo == orderSendBillNo);
- var has= all.Where(i => !string.IsNullOrEmpty(i.StatementBillNo));
- return (all.Count(), has.Count());
- }
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillMg),AuditLog("发货单管理查询")]
- public async Task<PagedResultDto<ViewOrderSendBill>> GetAllView(PagedRequestDto input)
- {
- var actionStopwatch = Stopwatch.StartNew();
- var query = FilterGetOrderSendBills(input);
- var totalCount = await AsyncQueryableExecuter.CountAsync(query);
- query = query.OrderByDescending(a => a.SendDate);
- query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
- var entities = await AsyncQueryableExecuter.ToListAsync(query);
- var dtos = new PagedResultDto<ViewOrderSendBill>(
- totalCount,
- entities
- );
- actionStopwatch.Stop();
- Debug.WriteLine(actionStopwatch.ElapsedMilliseconds);
- return dtos;
- }
- private IQueryable<ViewOrderSendBill> FilterGetOrderSendBills(PagedRequestDto input)
- {
- var query = ViewOrderSendBillRepository.GetAll();
- if (input.SearchList != null && input.SearchList.Count > 0)
- {
- List<LambdaObject> objList = new List<LambdaObject>();
- 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);
- }
- if (o.KeyField == "isbill")
- {
- if (keyWords.ToString() == "1")
- {
- query = query.Where(i => i.OrderSendCount >= 0 && i.StatementCount == 0);
- }
- else if (keyWords.ToString() == "2")
- {
- query = query.Where(i => i.OrderSendCount > 0 && i.StatementCount == i.OrderSendCount);
- }
- else if (keyWords.ToString() == "3")
- {
- query = query.Where(i => i.StatementCount > 0 && i.StatementCount < i.OrderSendCount);
- }
- continue;
- }
- objList.Add(new LambdaObject
- {
- FieldType = (LambdaFieldType)o.FieldType,
- FieldName = o.KeyField,
- FieldValue = keyWords,
- ExpType = (LambdaExpType)o.ExpType
- });
- }
- if (objList.Any())
- {
- var exp = objList.GetExp<ViewOrderSendBill>();
- query = query.Where(exp);
- }
- }
- return query;
- }
- public async Task<decimal[]> GetAllPrice(PagedRequestDto input)
- {
- var query = FilterGetOrderSendBills(input);
- var totalPrice = await query.SumAsync(i => i.TotalPrice);
- var afterTaxTotalPrice = await query.SumAsync(i => i.AfterTaxTotalPrice);
- decimal[] result = new[] {totalPrice ?? 0, afterTaxTotalPrice ?? 0};
- return result;
- }
- //#region MyRegion
- //[AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillMg), AuditLog("发货单管理查询")]
- //public async Task<PagedResultDto<ViewOrderSendBill>> GetAllView1(PagedRequestDto input)
- //{
- // string sqlWhere = "";
- // string isBill = "";
- // if (input.SearchList != null && input.SearchList.Count > 0)
- // {
- // foreach (var o in input.SearchList)
- // {
- // if (o.KeyWords.IsNullOrEmpty())
- // continue;
- // object keyWords = o.KeyWords;
- // if (o.KeyField == "id" )
- // {
- // sqlWhere += $" and OrderSendBillNo like '%{keyWords}%' ";
- // }
- // if (o.KeyField == "customerId")
- // {
- // sqlWhere += $" and CustomerId like '%{keyWords}%' ";
- // }
- // if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.LessOrEqual)
- // {
- // keyWords = Convert.ToDateTime(keyWords).AddDays(1);
- // sqlWhere += $" and SendDate <= '{keyWords:yyyy-MM-dd}' ";
- // }
- // if (o.KeyField == "sendDate" && (LambdaExpType)o.ExpType == LambdaExpType.GreaterOrEqual)
- // {
- // keyWords = Convert.ToDateTime(keyWords);
- // sqlWhere += $" and SendDate >= '{keyWords:yyyy-MM-dd}' ";
- // }
- // if (o.KeyField == "isbill")
- // {
- // isBill = keyWords.ToString();
- // }
- // }
- // }
-
- // string sql = "select OrderSendBillNo as Id,CustomerId,SendDate,ContactTels,ExpressName,ExpressId,ExpressBillNo,ContactMan, TotalPrice,AfterTaxTotalPrice,CurrencyId,OrderSendCount,StatementCount,SendAddress,TimeCreated,TimeLastMod,UserIDLastMod,IsDoBill,CreatorUserId from v_OrderSendBill where 1=1 " + sqlWhere;
- // var query = SqlExecuter.SqlQuery<ViewOrderSendBill>(sql);
- // var stQuery = OrderSendRepository.GetAll().Where(i => !string.IsNullOrEmpty(i.OrderSendBillNo));
- // foreach (var viewOrderSendBill in query)
- // {
- // var t = GetStatementCount(stQuery, viewOrderSendBill.Id);
- // viewOrderSendBill.StatementCount = t.Item2;
- // viewOrderSendBill.OrderSendCount = t.Item1;
- // }
- // if (!string.IsNullOrEmpty(isBill))
- // {
- // if (isBill == "1")
- // {
- // query = query.Where(i => i.OrderSendCount >= 0 && i.StatementCount == 0);
- // }
- // else if (isBill == "2")
- // {
- // query = query.Where(i => i.OrderSendCount > 0 && i.StatementCount == i.OrderSendCount);
- // }
- // else if (isBill == "3")
- // {
- // query = query.Where(i => i.StatementCount > 0 && i.StatementCount < i.OrderSendCount);
- // }
- // }
- // int totalCount = query.Count();
- // query = query.OrderByDescending(a => a.SendDate);
- // query = query.Skip(input.SkipCount).Take(input.MaxResultCount);
- // var entities =query.ToList();
- // var dtos = new PagedResultDto<ViewOrderSendBill>(
- // totalCount,
- // entities
- // );
- // return dtos;
- //}
- //#endregion
- [AbpAuthorize(PermissionNames.PagesSendGoodsOrderSendBillMgDelete),AuditLog("发货单撤销")]
- public override Task Delete(EntityDto<string> input)
- {
- CheckDeletePermission();
- var entity =Repository.Get(input.Id);
- var orderSends = OrderSendRepository.GetAllList(i => i.OrderSendBillNo == entity.Id);
- foreach (var send in orderSends)
- {
- send.OrderSendBillNo = "";
- send.OrderStickBillNo = "";
- OrderSendRepository.UpdateAsync(send);
- }
- BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository, "撤销发货单", $"发货单{entity.ToJsonString()}撤销");
- //this.Logger.Info("撤销发货单!"+entity.ToString());
- return Repository.DeleteAsync(input.Id);
- }
- public async Task UpdateExpressInfo(OrderSendBillExpressUpdateDto input)
- {
- var entity = Repository.Get(input.Id);
- entity.ExpressId = input.ExpressId;
- entity.ExpressBillNo = input.ExpressBillNo;
- await Repository.UpdateAsync(entity);
- }
- //public async Task<string> ExportOrderSend(EntityDto<string> input)
- //{
- // var bill = await Repository.FirstOrDefaultAsync(input.Id);
- // var orderSends =(await ViewOrderSendRepository.GetAllListAsync(i => i.OrderSendBillNo == input.Id)).OrderBy(i => i.SendBillSort).ThenByDescending(i => i.SendDate).ToList();
- // var customerInfo =await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId);
- // var templateInfo = await QueryAppService.QueryTemplate(bill.CustomerId, 2);
- // string[] classPath = templateInfo.ClassPath.Split("@@",StringSplitOptions.RemoveEmptyEntries);
- // if (!classPath.Any())
- // {
- // CheckErrors(IdentityResult.Failed("未查到对应的classPath"));
- // }
- // string className = classPath[0];
- // string methodName = classPath[1];
- // var tpType = Type.GetType(className);
- // object obj = Activator.CreateInstance(tpType ?? throw new InvalidOperationException());
- // Object[] paras = { bill, orderSends, customerInfo, templateInfo };
- // MethodInfo method = tpType.GetMethod(methodName);
- // var result = method?.Invoke(obj, paras);
- // return result?.ToString();
- //}
- // public async Task<string> ExportOrderSend(EntityDto<string> input)
- // {
- // var bill = await Repository.FirstOrDefaultAsync(input.Id);
- //}
- //处理特殊模板
- private void HandlerMiniDto(OrderSendMiniExcelDto miniValue, Inspection.TemplateInfo templateInfo)
- {
- string[] classPath = templateInfo.ClassPath.Split("@@", StringSplitOptions.RemoveEmptyEntries);
- if (!classPath.Any())
- {
- CheckErrors(IdentityResult.Failed("未查到对应的classPath"));
- }
- string className = classPath[0];
- string methodName = classPath[1];
- var tpType = Type.GetType(className);
- object obj = Activator.CreateInstance(tpType ?? throw new InvalidOperationException());
- Object[] paras = { miniValue };
- MethodInfo method = tpType.GetMethod(methodName);
- method?.Invoke(obj, paras);
- }
- public async Task<string> ExportOrderSendCommon(EntityDto<string> input)
- {
- var bill = await Repository.FirstOrDefaultAsync(input.Id);
- var orderSends =
- (await ViewOrderSendRepository.GetAllListAsync(i => i.OrderSendBillNo == input.Id)).OrderBy(i => i.SendBillSort).ThenByDescending(i => i.SendDate).ToList();
- var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId);
- var miniValue = ConvertToMiniExcelDto(bill,customerInfo,orderSends);
- string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/OrderSendTemplate/送货单模板.xlsx";
- var fileName = $"送货单-{Clock.Now:yyMMddHHmmss}.xlsx";
- if (orderSends.Any())
- {
- if (orderSends[0].SaleType == 2)
- {
- path = AppDomain.CurrentDomain.BaseDirectory + "Resources/OrderSendTemplate/外销送货单模板.xlsx";
- }
- }
- var templateInfo = await QueryAppService.QueryTemplate(bill.CustomerId, 2);
- if (templateInfo != null)
- {
- if (!templateInfo.ClassPath.IsNullOrEmpty())
- {
- HandlerMiniDto(miniValue, templateInfo);
- }
- path = AppDomain.CurrentDomain.BaseDirectory + templateInfo.FilePath;
- fileName = templateInfo.Name + fileName;
- }
- var savePath = "Download/Excel/OrderSendBill";
- savePath = Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
- string resultPath = $"/Download/Excel/OrderSendBill/{fileName}";
- //var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
- try
- {
- await MiniExcel.SaveAsByTemplateAsync(savePath, path, miniValue);
- }
- catch (Exception err)
- {
- CheckErrors(new IdentityResult(err.Message));
- }
- return resultPath;
- }
- private OrderSendMiniExcelDto ConvertToMiniExcelDto(OrderSendBill bill,Customer customerInfo,List<ViewOrderSend> orderSends)
- {
- OrderSendMiniExcelDto miniValue = new OrderSendMiniExcelDto()
- {
- Ex1 = DateTime.Now.ToString("yyyy-MM-dd"),
- Ex2 = DateTime.Now.ToString("yyyy年MM月dd日"),
- OrderSendBillNo = bill.Id,
- SendAddress = bill.SendAddress,
- CustomerName = customerInfo.CustomerName,
- ContactTels = bill.ContactTels,
- ContactMan = bill.ContactMan
- };
- miniValue.SendDate = bill.SendDate;
- int index = 0;
- decimal allPackageCount = 0;
- decimal allSendQuantity = 0;
- decimal allTotalPrice = 0;
- List<OrderSendItemExcelDto> orderSendItem = new List<OrderSendItemExcelDto>();
- foreach (var send in orderSends)
- {
- OrderSendItemExcelDto item = new OrderSendItemExcelDto();
- var sendQuantity = Math.Round(Math.Floor(send.SendQuantity * 1000) / 1000, 3);//发货数量
- allSendQuantity += sendQuantity;
- var quantityPerPack = send.QuantityPerPack ?? 0;
- quantityPerPack = quantityPerPack == 0 ? sendQuantity : quantityPerPack;//每包数量,如果为0则等于发货数量
- decimal packageCount = sendQuantity == 0 ? 0 : (sendQuantity <= quantityPerPack ? 1 : Math.Floor(send.SendQuantity / quantityPerPack));
- var sysl = sendQuantity - quantityPerPack * packageCount;
- allPackageCount += packageCount + (sysl > 0 ? 1 : 0);
- item.Index = index + 1;
- item.StockNo = send.StockNo ?? send.OrderNo;
- item.PartNo = send.PartNo ?? "";
- item.ProductName = send.ProductName ?? "";
- item.Model = send.Model ?? "";
- item.SurfaceColor = send.SurfaceColor ?? "";
- item.Material = send.Material ?? "";
- item.Rigidity = send.Rigidity ?? "";
- item.Unit = "千件";
- item.SendQuantity = sendQuantity;
- item.PackageCount = packageCount;
- item.QuantityPerPack = quantityPerPack;
- item.Sysl = sysl;
- item.ProductBatchNum = send.ProductBatchNum ?? "";
- item.SendDate = send.SendDate;
- item.Remark = send.Remark;
- item.Price = send.Price;
- item.AfterTaxPrice = send.AfterTaxPrice;
- allTotalPrice += send.TotalPrice;
- orderSendItem.Add(item);
- index++;
- }
- miniValue.AllPackageCount = allPackageCount;
- miniValue.AllSendQuantity = allSendQuantity;
- miniValue.AllTotalPrice = allTotalPrice;
- miniValue.send = orderSendItem;
- return miniValue;
- }
- //public async Task<string> ExportOrderSendCommon2(EntityDto<string> input)
- // {
- // var bill = await Repository.FirstOrDefaultAsync(input.Id);
- // //var orderSends = (await ViewOrderSendRepository.GetAllListAsync(i => i.OrderSendBillNo == input.Id)).OrderBy(i=>i.SurfaceColor).ThenBy(i=>i.Rigidity).ToList();
- // var orderSends =
- // (await ViewOrderSendRepository.GetAllListAsync(i => i.OrderSendBillNo == input.Id)).OrderBy(i => i.SendBillSort).ThenByDescending(i=>i.SendDate).ToList();
- // var customerInfo = await CustomerRepository.FirstOrDefaultAsync(bill.CustomerId);
- // if (orderSends.Any())
- // {
- // if (orderSends[0].SaleType == 2)
- // {
- // return ExportOutSale(orderSends, bill, customerInfo);
- // }
- // }
- // string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/OrderSendTemplate/送货单模板.xlsx";
- // var savePath = "Download/Excel/OrderSendBill";
- // var work = ExcelHelper.CreateWorkBook07(path);
- // var sheet1 = work.GetSheet("Sheet1");
- // sheet1.GenerateCell(2, 1).SetCellValue("单号:"+bill.Id);
- // sheet1.GenerateCell(5, 2).SetCellValue("客户:" + customerInfo.CustomerName);
- // sheet1.GenerateCell(5, 10).SetCellValue("地址:" + bill.SendAddress);
- // sheet1.GenerateCell(6, 2).SetCellValue("联系电话:" + bill.ContactTels);
- // sheet1.GenerateCell(6, 10).SetCellValue("联系人:" + bill.ContactMan);
- // sheet1.GenerateCell(7, 1).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日"));
- // sheet1.InsertRows(10, orderSends.Count);
- // int index = 0;
- // decimal allPackageCount = 0;
- // foreach (var send in orderSends)
- // {
- // var sendQuantity = Math.Round(Math.Floor(send.SendQuantity * 1000) / 1000, 3);//发货数量
- // var quantityPerPack = send.QuantityPerPack ?? 0;
- // quantityPerPack = quantityPerPack == 0 ? sendQuantity : quantityPerPack;//每包数量,如果为0则等于发货数量
- // decimal packageCount = sendQuantity == 0 ? 0 : (sendQuantity <= quantityPerPack ? 1 : Math.Floor(send.SendQuantity / quantityPerPack));
- // var sysl = sendQuantity - quantityPerPack * packageCount;
- // allPackageCount += packageCount+(sysl>0?1:0);
- // sheet1.GenerateCell(10 + index, 1).SetValue<int>(index + 1);
- // sheet1.GenerateCell(10 + index, 2).SetValue(send.StockNo?? send.OrderNo);
- // sheet1.GenerateCell(10 + index, 3).SetValue(send.PartNo ?? "");
- // sheet1.GenerateCell(10 + index, 4).SetValue(send.ProductName??"");
- // sheet1.GenerateCell(10 + index, 5).SetValue(send.Model ?? "");
- // sheet1.GenerateCell(10 + index, 6).SetValue(send.SurfaceColor ?? "");
- // sheet1.GenerateCell(10 + index, 7).SetValue(send.Material ?? "");
- // sheet1.GenerateCell(10 + index, 8).SetValue(send.Rigidity ?? "");
- // sheet1.GenerateCell(10 + index, 9).SetValue("千件");
- // sheet1.GenerateCell(10 + index, 10).SetValue<decimal>(sendQuantity);
- // sheet1.GenerateCell(10 + index, 11).SetValue<decimal>(packageCount);
- // sheet1.GenerateCell(10 + index, 12).SetValue<decimal>(quantityPerPack);
- // sheet1.GenerateCell(10 + index, 13).SetValue<decimal>(sysl);
- // sheet1.GenerateCell(10 + index, 14).SetValue<decimal>(sendQuantity);
- // sheet1.GenerateCell(10 + index, 15).SetValue(send.ProductBatchNum??"");
- // index++;
- // }
- // index++;
- // sheet1.GenerateCell(10 + index, 8).SetValue( "合计: 托盘、"+ allPackageCount + "箱、待进仓");
- // index+=3;
- // sheet1.GenerateCell(10 + index, 12).SetValue( "送货日期:"+DateTime.Now.ToString("yyyy-MM-dd"));
- // 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}";
- // }
- //private string ExportOutSale(List<ViewOrderSend> orderSends, OrderSendBill bill,
- // Customer customerInfo)
- //{
- // string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/OrderSendTemplate/外销送货单模板2.xlsx";
- // OrderSendMiniExcelDto miniValue = new OrderSendMiniExcelDto()
- // {
- // Ex1 = DateTime.Now.ToString("yyyy-MM-dd"),
- // OrderSendBillNo = bill.Id,
- // SendAddress = bill.SendAddress,
- // CustomerName = customerInfo.CustomerName
- // };
- // var savePath = "Download/Excel/OrderSendBill";
- // var fileName = $"送货单-{Clock.Now:yyMMddHHmmss}.xlsx";
- // int index = 0;
- // decimal allPackageCount = 0;
- // decimal allSendQuantity = 0;
- // decimal allTotalPrice = 0;
- // List<OrderSendItemExcelDto> orderSendItem = new List<OrderSendItemExcelDto>();
- // foreach (var send in orderSends)
- // {
- // OrderSendItemExcelDto item = new OrderSendItemExcelDto();
- // var sendQuantity = Math.Round(Math.Floor(send.SendQuantity * 1000) / 1000, 3);//发货数量
- // allSendQuantity += sendQuantity;
- // var quantityPerPack = send.QuantityPerPack ?? 0;
- // quantityPerPack = quantityPerPack == 0 ? sendQuantity : quantityPerPack;//每包数量,如果为0则等于发货数量
- // decimal packageCount = sendQuantity == 0 ? 0 : (sendQuantity <= quantityPerPack ? 1 : Math.Floor(send.SendQuantity / quantityPerPack));
- // var sysl = sendQuantity - quantityPerPack * packageCount;
- // allPackageCount += packageCount + (sysl > 0 ? 1 : 0);
- // item.Index = index + 1;
- // item.StockNo = send.StockNo ?? send.OrderNo;
- // item.PartNo = send.PartNo ?? "";
- // item.ProductName = send.ProductName ?? "";
- // item.Model = send.Model ?? "";
- // item.SurfaceColor = send.SurfaceColor ?? "";
- // item.Material = send.Material ?? "";
- // item.Rigidity = send.Rigidity ?? "";
- // item.Unit = "千件";
- // item.SendQuantity = sendQuantity;
- // item.PackageCount = packageCount;
- // item.QuantityPerPack = quantityPerPack;
- // item.Sysl = sysl;
- // item.ProductBatchNum = send.ProductBatchNum ?? "";
- // allTotalPrice += send.TotalPrice;
- // orderSendItem.Add(item);
- // index++;
- // }
- // miniValue.AllPackageCount = allPackageCount;
- // miniValue.AllSendQuantity = allSendQuantity;
- // miniValue.AllTotalPrice = allTotalPrice;
- // miniValue.send=orderSendItem;
- // savePath = Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
- // string resultPath = $"/Download/Excel/OrderSendBill/{fileName}";
- // //var result = work?.SaveWorkBook($"{AppDomain.CurrentDomain.BaseDirectory}{savePath}", fileName);
- // try
- // {
- // MiniExcel.SaveAsByTemplate(savePath, path, miniValue);
- // }
- // catch (Exception err)
- // {
- // CheckErrors(new IdentityResult(err.Message));
- // }
- // return resultPath;
- //}
- //private string ExportOutSale2(List<ViewOrderSend> orderSends, OrderSendBill bill,
- //Customer customerInfo)
- //{
- // string path = AppDomain.CurrentDomain.BaseDirectory + "Resources/OrderSendTemplate/外销送货单模板.xlsx";
- // var savePath = "Download/Excel/OrderSendBill";
- // var work = ExcelHelper.CreateWorkBook07(path);
- // var sheet1 = work.GetSheet("Sheet1");
- // sheet1.GenerateCell(2, 1).SetCellValue("单号:" + bill.Id);
- // sheet1.GenerateCell(5, 1).SetCellValue("Buyer Name:" + customerInfo.CustomerName);
- // sheet1.GenerateCell(6, 1).SetCellValue("Buyer Add: " + bill.SendAddress);
- // //sheet1.GenerateCell(6, 2).SetCellValue("联系电话:" + bill.ContactTels);
- // //sheet1.GenerateCell(6, 10).SetCellValue("联系人:" + bill.ContactMan);
- // //sheet1.GenerateCell(7, 1).SetCellValue("日期:" + DateTime.Now.ToString("yyyy年MM月dd日"));
- // sheet1.InsertRows(14, orderSends.Count);
- // int index = 0;
- // decimal allPackageCount = 0;
- // decimal allSendQuantity = 0;
- // decimal allTotalPrice = 0;
- // foreach (var send in orderSends)
- // {
- // var sendQuantity = Math.Round(Math.Floor(send.SendQuantity * 1000) / 1000, 3);//发货数量
- // allSendQuantity += sendQuantity;
- // var quantityPerPack = send.QuantityPerPack ?? 0;
- // quantityPerPack = quantityPerPack == 0 ? sendQuantity : quantityPerPack;//每包数量,如果为0则等于发货数量
- // decimal packageCount = sendQuantity == 0 ? 0 : (sendQuantity <= quantityPerPack ? 1 : Math.Floor(send.SendQuantity / quantityPerPack));
- // var sysl = sendQuantity - quantityPerPack * packageCount;
- // allPackageCount += packageCount + (sysl > 0 ? 1 : 0);
- // sheet1.GenerateCell(14 + index, 1).SetValue<int>(index + 1);
- // sheet1.GenerateCell(14 + index, 2).SetValue(send.StockNo ?? send.OrderNo);
- // sheet1.GenerateCell(14 + index, 3).SetValue(send.PartNo ?? "");
- // sheet1.GenerateCell(14 + index, 4).SetValue(send.ProductName ?? "");
- // sheet1.GenerateCell(14 + index, 5).SetValue(send.Model ?? "");
- // sheet1.GenerateCell(14 + index, 6).SetValue(send.SurfaceColor ?? "");
- // sheet1.GenerateCell(14 + index, 7).SetValue(send.Material ?? "");
- // sheet1.GenerateCell(14 + index, 8).SetValue(send.Rigidity ?? "");
- // sheet1.GenerateCell(14 + index, 9).SetValue("千件");
- // sheet1.GenerateCell(14 + index, 10).SetValue<decimal>(sendQuantity);
- // sheet1.GenerateCell(14 + index, 11).SetValue<decimal>(packageCount);
- // sheet1.GenerateCell(14 + index, 12).SetValue<decimal>(quantityPerPack);
- // sheet1.GenerateCell(14 + index, 13).SetValue<decimal>(sysl);
- // sheet1.GenerateCell(14 + index, 14).SetValue<decimal>(sendQuantity);
- // sheet1.GenerateCell(14 + index, 15).SetValue(send.ProductBatchNum ?? "");
- // //sheet1.GenerateCell(14 + index, 16).SetValue<decimal>(send.Price);
- // //sheet1.GenerateCell(14 + index, 17).SetValue<decimal>(send.TotalPrice);
- // allTotalPrice += send.TotalPrice;
- // index++;
- // }
- // index += 4;
- // sheet1.GenerateCell(14 + index, 17).SetValue<decimal>(allSendQuantity);
- // index += 1;
- // //sheet1.GenerateCell(14 + index, 17).SetValue<decimal>(allTotalPrice);
- // index += 3;
- // sheet1.GenerateCell(14 + index, 12).SetValue("送货日期:" + DateTime.Now.ToString("yyyy-MM-dd"));
- // 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}";
- //}
- #endregion
- }
- }
|