步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

518 lines
21 KiB

3 years ago
using BBWY.Client.APIServices;
using BBWY.Client.Models;
3 years ago
using BBWY.Client.Views.BillCorrection;
3 years ago
using BBWY.Common.Models;
3 years ago
using BBWY.Common.Trigger;
3 years ago
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
3 years ago
using Newtonsoft.Json;
3 years ago
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
3 years ago
using System.Threading.Tasks;
3 years ago
using System.Windows;
using System.Windows.Input;
3 years ago
using BBWY.Common.Extensions;
3 years ago
namespace BBWY.Client.ViewModels
{
public class BillCorrectionViewModel : BaseVM, IDenpendency
{
3 years ago
private BillCorrectionService billCorrectionService;
3 years ago
private string searchShopKeyWord;
3 years ago
private DelayTrigger delayTrigger;
private bool isLoading;
3 years ago
private KVModel selectedLocalFilterOperation;
private bool isEnableCanCorrectionOrder;
private StorageType? selectedStorageType;
private string searchLocalOrderId;
private Shop selectedLocalShop;
3 years ago
3 years ago
private GlobalContext globalContext;
3 years ago
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
/// <summary>
/// 销售运费快递账单文件列表
/// </summary>
public ObservableCollection<string> SaleFreightBillFileList { get; set; }
/// <summary>
/// 销售运费账单列表
/// </summary>
public List<BillModel> SaleFreightBillList { get; set; }
3 years ago
public IList<Shop> ShopList { get; set; }
3 years ago
public IList<Shop> LocalShopList { get; set; }
3 years ago
/// <summary>
3 years ago
/// 店铺搜索关键词
3 years ago
/// </summary>
3 years ago
public string SearchShopKeyWord
{
get => searchShopKeyWord;
set
{
if (Set(ref searchShopKeyWord, value))
{
delayTrigger.SetKey(value);
}
}
}
3 years ago
3 years ago
/// <summary>
3 years ago
/// 导入快递账单
3 years ago
/// </summary>
3 years ago
public ICommand ImportSaleFreightBillCommand { get; set; }
3 years ago
/// <summary>
3 years ago
/// 查询订单
3 years ago
/// </summary>
3 years ago
public ICommand SearchBillCorrectionOrderCommand { get; set; }
3 years ago
3 years ago
/// <summary>
/// 清空数据
/// </summary>
public ICommand ClearCommand { get; set; }
3 years ago
/// <summary>
/// 矫正费用
/// </summary>
public ICommand CorrectCommand { get; set; }
3 years ago
public ICommand SearchLocalCommand { get; set; }
public ICommand ClearLocalConditionCommand { get; set; }
/// <summary>
/// 保存
/// </summary>
public ICommand SaveCommand { get; set; }
3 years ago
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
public IList<BillCorrectionOrder> OrderList { get; set; }
3 years ago
3 years ago
public IList<BillCorrectionOrder> LocalOrderList { get; set; }
public IList<KVModel> LocalFilterOperationList { get; set; }
public KVModel SelectedLocalFilterOperation { get => selectedLocalFilterOperation; set { Set(ref selectedLocalFilterOperation, value); } }
public bool IsEnableCanCorrectionOrder { get => isEnableCanCorrectionOrder; set { Set(ref isEnableCanCorrectionOrder, value); } }
public StorageType? SelectedStorageType { get => selectedStorageType; set { Set(ref selectedStorageType, value); } }
public string SearchLocalOrderId { get => searchLocalOrderId; set { Set(ref searchLocalOrderId, value); } }
public Shop SelectedLocalShop { get => selectedLocalShop; set { Set(ref selectedLocalShop, value); } }
3 years ago
public BillCorrectionViewModel(GlobalContext globalContext, BillCorrectionService billCorrectionService)
3 years ago
{
3 years ago
this.billCorrectionService = billCorrectionService;
this.globalContext = globalContext;
var shopList = JsonConvert.DeserializeObject<List<Shop>>(JsonConvert.SerializeObject(globalContext.User.ShopList));
this.ShopList = new ObservableCollection<Shop>(shopList);
3 years ago
this.LocalShopList = new ObservableCollection<Shop>();
3 years ago
SaleFreightBillFileList = new ObservableCollection<string>();
SaleFreightBillList = new List<BillModel>();
StartDate = DateTime.Now.Date.AddDays((DateTime.Now.Day - 1) * -1).AddMonths(-1);
EndDate = StartDate.AddMonths(1).AddDays(-1);
3 years ago
delayTrigger = new DelayTrigger(500);
delayTrigger.OnExecute = OnSearchShopKeyWordChanged;
OrderList = new ObservableCollection<BillCorrectionOrder>();
3 years ago
LocalOrderList = new ObservableCollection<BillCorrectionOrder>();
LocalFilterOperationList = new List<KVModel>()
{
new KVModel(){ Key="保留",Value="save" },
new KVModel(){ Key="过滤",Value="filter"}
};
3 years ago
ImportSaleFreightBillCommand = new RelayCommand<string>(ImportSaleFreightBill);
3 years ago
SearchBillCorrectionOrderCommand = new RelayCommand(SearchBillCorrectionOrder);
3 years ago
CorrectCommand = new RelayCommand(Correct);
3 years ago
ClearCommand = new RelayCommand(Clear);
SearchLocalCommand = new RelayCommand(SearchLocalOrderList);
ClearLocalConditionCommand = new RelayCommand(ClearLocalCondition);
SaveCommand = new RelayCommand(Save);
SelectedLocalFilterOperation = LocalFilterOperationList[0];
3 years ago
}
private void OnSearchShopKeyWordChanged(string key)
{
foreach (var s in this.ShopList)
s.IsSelected = false;
App.Current.Dispatcher.Invoke(() =>
{
this.ShopList.Clear();
var keyWordShopList = string.IsNullOrEmpty(key) ? globalContext.User.ShopList : globalContext.User.ShopList.Where(s => s.ShopName.Contains(key));
if (keyWordShopList.Count() > 0)
{
foreach (var shop in keyWordShopList)
ShopList.Add(shop);
}
});
3 years ago
}
private void ImportSaleFreightBill(string expressName)
{
var ofd = new OpenFileDialog() { Filter = "excel文件|*.xlsx;*.xls" };
if (ofd.ShowDialog() != true)
return;
var fileName = ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1);
var filePath = ofd.FileName;
if (SaleFreightBillFileList.Contains(fileName))
{
MessageBox.Show("请勿重复导入快递账单", "导入快递账单");
return;
}
IWorkbook xbook = null;
try
{
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xls"))
xbook = new HSSFWorkbook(fs);
else if (filePath.EndsWith(".xlsx"))
xbook = new XSSFWorkbook(fs);
}
using (xbook)
{
IList<BillModel> billModelList = null;
if (expressName == "YT")
{
3 years ago
var basicAmountW = new YTBasicAmount();
if (basicAmountW.ShowDialog() != true)
return;
var basicAmount = basicAmountW.BasicAmount;
billModelList = LoadYTSaleBillFile(xbook, fileName, basicAmount);
3 years ago
}
else if (expressName == "YZ")
{
3 years ago
billModelList = LoadYZSaleBillFile(xbook, fileName);
3 years ago
}
else if (expressName == "JD")
{
3 years ago
billModelList = LoadJDSaleBillFile(xbook, fileName);
3 years ago
}
if (billModelList != null && billModelList.Count() > 0)
{
SaleFreightBillList.AddRange(billModelList);
SaleFreightBillFileList.Add(fileName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "导入账单失败提示");
return;
}
}
3 years ago
/// <summary>
/// 读取圆通运费账单
/// </summary>
/// <param name="xbook"></param>
/// <param name="belongFileName"></param>
/// <param name="basicSaleFreight">基础面单价格</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private IList<BillModel> LoadYTSaleBillFile(IWorkbook xbook, string belongFileName, decimal basicSaleFreight)
3 years ago
{
3 years ago
var sheet = xbook.GetSheetAt(0);
var waybillNoCellTitle = sheet.GetRow(0).GetCell(1);
if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "运单号码")
throw new Exception("验证圆通快递账单失败-未读取到运单号码");
var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(5);
if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "补差")
throw new Exception("验证圆通快递账单失败-未读取到补差");
var rowCount = sheet.LastRowNum;
IList<BillModel> list = new List<BillModel>();
for (var i = 1; i < rowCount; i++)
{
var row = sheet.GetRow(i);
if (row == null)
break;
var waybillNoCell = row.GetCell(1);
if (string.IsNullOrEmpty(waybillNoCell.StringCellValue))
break;
var saleExpressFreightCell = row.GetCell(5);
list.Add(new BillModel()
{
Amount = Convert.ToDecimal(saleExpressFreightCell.NumericCellValue) + basicSaleFreight,
BillNo = waybillNoCell.StringCellValue,
BillType = BillCorrectionType.,
BelongFileName = belongFileName
});
}
return list;
3 years ago
}
/// <summary>
/// 读取邮政运费账单
/// </summary>
/// <param name="xbook"></param>
3 years ago
/// <param name="belongFileName"></param>
3 years ago
/// <returns></returns>
3 years ago
/// <exception cref="Exception"></exception>
private IList<BillModel> LoadYZSaleBillFile(IWorkbook xbook, string belongFileName)
3 years ago
{
var sheet = xbook.GetSheetAt(0);
var waybillNoCellTitle = sheet.GetRow(0).GetCell(2);
if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "邮件号")
throw new Exception("验证邮政快递账单失败-未读取到邮件号");
3 years ago
var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(8);
if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "总邮资")
3 years ago
throw new Exception("验证邮政快递账单失败-未读取到总邮资");
var rowCount = sheet.LastRowNum;
3 years ago
IList<BillModel> list = new List<BillModel>();
3 years ago
for (var i = 1; i < rowCount; i++)
3 years ago
{
var row = sheet.GetRow(i);
if (row == null)
break;
var waybillNoCell = row.GetCell(2);
if (string.IsNullOrEmpty(waybillNoCell.StringCellValue))
break;
var saleExpressFreightCell = row.GetCell(8);
list.Add(new BillModel()
{
Amount = Convert.ToDecimal(saleExpressFreightCell.NumericCellValue),
BillNo = waybillNoCell.StringCellValue,
BillType = BillCorrectionType.,
BelongFileName = belongFileName
});
3 years ago
}
3 years ago
return list;
3 years ago
}
3 years ago
/// <summary>
/// 读取JD运费账单
/// </summary>
/// <param name="xbook"></param>
/// <param name="belongFileName"></param>
/// <returns></returns>
3 years ago
private IList<BillModel> LoadJDSaleBillFile(IWorkbook xbook, string belongFileName)
3 years ago
{
3 years ago
var sheet = xbook.GetSheetAt(1);
if (sheet == null)
throw new Exception("验证JD快递账单失败-未读取到sheet1");
var waybillNoCellTitle = sheet.GetRow(0).GetCell(2);
if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "运单号")
throw new Exception("验证JD快递账单失败-未读取到运单号");
var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(35);
if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "结算金额")
throw new Exception("验证JD快递账单失败-未读取到结算金额");
var rowCount = sheet.LastRowNum;
IList<BillModel> list = new List<BillModel>();
for (var i = 1; i < rowCount; i++)
{
var row = sheet.GetRow(i);
if (row == null)
break;
var waybillNoCell = row.GetCell(2);
if (string.IsNullOrEmpty(waybillNoCell.StringCellValue))
break;
var saleExpressFreightCell = row.GetCell(35);
var b = list.FirstOrDefault(b => b.BillNo == waybillNoCell.StringCellValue);
if (b == null)
{
b = new BillModel()
{
BelongFileName = belongFileName,
BillNo = waybillNoCell.StringCellValue,
BillType = BillCorrectionType.
};
list.Add(b);
}
b.Amount += Convert.ToDecimal(saleExpressFreightCell.NumericCellValue);
}
return list;
3 years ago
}
3 years ago
/// <summary>
/// 查询
/// </summary>
private void SearchBillCorrectionOrder()
{
OrderList.Clear();
3 years ago
LocalShopList.Clear();
var selectShops = ShopList.Where(s => s.IsSelected).ToList();
if (selectShops.Count() == 0)
{
MessageBox.Show("至少选择一个店铺", "提示");
return;
}
IsLoading = true;
foreach (var shop in LocalShopList)
LocalShopList.Add(shop);
SelectedLocalShop = null;
var shopIds = selectShops.Select(s => s.ShopId).ToList();
3 years ago
Task.Factory.StartNew(() => billCorrectionService.GetBillCorrectionOrderList(shopIds, StartDate, EndDate))
.ContinueWith(t =>
{
IsLoading = false;
var response = t.Result;
if (!response.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "提示"));
return;
}
if (response.Data == null || response.Data.Count() == 0)
return;
var list = response.Data.Map<IList<BillCorrectionOrder>>();
3 years ago
App.Current.Dispatcher.Invoke(() =>
3 years ago
{
foreach (var order in list)
{
3 years ago
//if (string.IsNullOrEmpty(order.WaybillNo))
// continue;
//var billModel = SaleFreightBillList.FirstOrDefault(b => b.BillNo == order.WaybillNo);
//if (billModel == null)
// continue;
//if (billModel.Amount != order.DeliveryExpressFreight)
//{
// order.NewDeliveryExpressFreight = billModel.Amount;
// order.ChangedContent = $"销售运费 {order.DeliveryExpressFreight} -> {order.NewDeliveryExpressFreight}";
//}
3 years ago
OrderList.Add(order);
}
3 years ago
SearchLocalOrderList();
3 years ago
});
});
}
3 years ago
3 years ago
private void SearchLocalOrderList()
{
LocalOrderList.Clear();
var where = OrderList.Where(o => true);
if (SelectedLocalShop != null)
where = where.Where(o => o.ShopId == SelectedLocalShop.ShopId);
if (SelectedStorageType != null)
where = where.Where(o => o.StorageType == SelectedStorageType);
if (!string.IsNullOrEmpty(SearchLocalOrderId))
where = where.Where(o => o.OrderId == SearchLocalOrderId);
if (IsEnableCanCorrectionOrder)
{
if (SelectedLocalFilterOperation.Value == "save")
where = where.Where(o => !string.IsNullOrEmpty(o.ChangedContent));
else
where = where.Where(o => string.IsNullOrEmpty(o.ChangedContent));
}
var list = where.ToList();
foreach (var o in list)
LocalOrderList.Add(o);
}
3 years ago
/// <summary>
/// 矫正费用
/// </summary>
private void Correct()
{
3 years ago
if (OrderList.Count() == 0)
{
MessageBox.Show("缺少待矫正订单");
return;
}
if (SaleFreightBillList.Count() == 0)
{
MessageBox.Show("缺少运费账单");
return;
}
3 years ago
3 years ago
foreach (var order in OrderList)
{
//矫正销售运费
if (!string.IsNullOrEmpty(order.WaybillNo))
{
var expressBillModel = SaleFreightBillList.FirstOrDefault(b => b.BillNo == order.WaybillNo);
if (expressBillModel != null && expressBillModel.Amount != order.DeliveryExpressFreight)
{
order.NewDeliveryExpressFreight = expressBillModel.Amount;
order.ChangedContent = $"销售运费{order.DeliveryExpressFreight}->{order.NewDeliveryExpressFreight} ";
}
}
//矫正入仓操作
//矫正出仓操作
}
3 years ago
var correctionCount = OrderList.Count(o => !string.IsNullOrEmpty(o.ChangedContent));
MessageBox.Show($"矫正完成,本次矫正{correctionCount}笔订单", "矫正费用");
3 years ago
}
private void Clear()
{
SaleFreightBillFileList.Clear();
SaleFreightBillList.Clear();
OrderList.Clear();
LocalOrderList.Clear();
SelectedStorageType = null;
IsEnableCanCorrectionOrder = false;
SearchShopKeyWord = string.Empty;
SearchLocalOrderId = string.Empty;
SelectedLocalShop = null;
SelectedLocalFilterOperation = LocalFilterOperationList[0];
}
private void ClearLocalCondition()
{
SelectedStorageType = null;
IsEnableCanCorrectionOrder = false;
SearchLocalOrderId = string.Empty;
SelectedLocalShop = null;
SearchLocalOrderList();
}
private void Save()
{
var saveOrderList = OrderList.Where(o => !string.IsNullOrEmpty(o.ChangedContent));
if (saveOrderList.Count() == 0)
{
MessageBox.Show("没有需要矫正的订单", "提示");
return;
}
3 years ago
var billCorrectionOrderList = saveOrderList.Select(o => new BillCorrectionRequest()
{
OrderId = o.OrderId,
NewDeliveryExpressFreight = o.NewDeliveryExpressFreight
}).ToList();
IsLoading = true;
Task.Factory.StartNew(() => billCorrectionService.CorrectOrder(billCorrectionOrderList))
.ContinueWith(t =>
{
IsLoading = false;
var response = t.Result;
if (!response.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "矫正费用 "));
return;
}
App.Current.Dispatcher.Invoke(() => MessageBox.Show("矫正账单成功", "矫正费用"));
this.Clear();
});
3 years ago
}
3 years ago
}
}