步步为盈
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.

246 lines
10 KiB

3 years ago
using BBWY.Client.Models;
3 years ago
using BBWY.Client.Views.BillCorrection;
3 years ago
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
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;
using System.Windows;
using System.Windows.Input;
namespace BBWY.Client.ViewModels
{
public class BillCorrectionViewModel : BaseVM, IDenpendency
{
3 years ago
private bool isShowShopKeyword;
private string searchShopKeyWord;
3 years ago
public GlobalContext GlobalContext { get; set; }
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; }
/// <summary>
/// 导入快递账单
/// </summary>
public ICommand ImportSaleFreightBillCommand { get; set; }
3 years ago
/// <summary>
/// 是否显示店铺搜索关键词
/// </summary>
public bool IsShowShopKeyword { get => isShowShopKeyword; set { Set(ref isShowShopKeyword, value); } }
public IList<ShopResponse> ShopList { get; set; }
/// <summary>
/// 店铺搜索关键词
/// </summary>
public string SearchShopKeyWord { get => searchShopKeyWord; set { Set(ref searchShopKeyWord, value); } }
public BillCorrectionViewModel(GlobalContext globalContext)
3 years ago
{
3 years ago
this.GlobalContext = globalContext;
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);
ImportSaleFreightBillCommand = new RelayCommand<string>(ImportSaleFreightBill);
}
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
}
}
}