发票助手修改,增加时间范围,增加上次读取历史,修复bug

This commit is contained in:
chfei committed 2026-09-18 14:32:52 +08:00
1 parent b42c17c013
commit 9e62672013
8 files changed
+729 -218

No files matched your search

@@ -2,6 +2,7 @@ using _29WxOrderWpf.BaseWindows;
using _29WxOrderWpf.CefHelper;
using _29WxOrderWpf.Commands;
using _29WxOrderWpf.Common;
using _29WxOrderWpf.Ctrls.Statistics;
using AllBase;
using AllBase.Helper;
using AllBase.Model.DBModel;
@@ -32,7 +33,9 @@ using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using Work._29Tools.CefTools;
using Work.BaseForms;
using WPFCommon.Converters;
using WPFCommon.Helpers;
using WPFCommon.Models;
using WxOrder.Framework.Helpers;
@@ -44,7 +47,7 @@ namespace _29WxOrderWpf.Ctrls.PDDInvoice.ViewModels;
public partial class PDDInvoiceViewModel : BindableBaseEx
{
private const string ConfigPath = "./invoice.config";
private readonly ObservableCollection<PddBrowserOrderInvoiceViewModel> _allOrders = new();
private List<OrderInvoiceInfo> OrderInvoiceInfoList = new();
private readonly HashSet<string> _orderSns = new();
private string _searchText = "";
private PddBrowserOrderInvoiceViewModel _selectedOrder;
@@ -53,14 +56,18 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
private string _invoiceTitle = "";
private string _taxNo = "";
private CancellationTokenSource _cancellationTokenSource;
private InvoiceConfig Config { get; }
private InvoiceConfig Config { get; set; }
/// <summary> 订单列表 </summary>
public ObservableCollection<PddBrowserOrderInvoiceViewModel> Orders { get; }
public ICollectionView OrdersView { get; }
public PDDInvoiceViewModel()
{
_cancellationTokenSource = new CancellationTokenSource();
Config = NaiBa.Util.Helper.ConfigHelper.LoadConfig<InvoiceConfig>(ConfigPath);
Orders = new ObservableCollection<PddBrowserOrderInvoiceViewModel>();
Orders.CollectionChanged += Orders_CollectionChanged;
OrdersView =CollectionViewSource.GetDefaultView(Orders);
OrdersView = CollectionViewSource.GetDefaultView(Orders);
OrdersView.Filter = FilterPredicate;
OrdersView.SortDescriptions.Add(
new SortDescription(nameof(PddBrowserOrderInvoiceViewModel.order_time), ListSortDirection.Ascending));
@@ -70,18 +77,33 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
InitBrowser();
if (Config == null) Config = new InvoiceConfig();
InvoiceTitle = Config.Title;
TaxNo = Config.TaxNo;
MaxAmount = Config.MaxAmount;
EndTime = DateTime.Now;
StartTime = EndTime.AddMonths(-1);
if (EnvironmentHelper.IsTest)
{
InvoiceTitle = "嵩县雷神商贸有限责任公司";
TaxNo = "91410325MADQERT68E";
MaxAmount = 99;
Config.Title = "嵩县雷神商贸有限责任公司";
Config.TaxNo = "91410325MADQERT68E";
Config.MaxAmount = 99;
_ = InitSampleData();
}
}
private bool FilterPredicate(object obj)
{
if (obj is not PddBrowserOrderInvoiceViewModel order) return false;
// 发票状态过滤
bool? temp = InvoiceStatus == 0 ? null : InvoiceStatus == 2;
if (temp.HasValue && order.IsInvoiced != temp.Value)
return false;
// 关键字过滤
var keyword = SearchText?.Trim();
if (!IsKeywordMatch(order, keyword))
return false;
if (order.OrderTime < StartTime || EndTime < order.OrderTime) return false;
return true;
}
partial void InitBrowser();
/// <summary> 列表增删时订阅/退订订单勾选状态,用于刷新表头全选状态 </summary>
@@ -118,11 +140,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
public ObservableCollectionThreadSafe<BuyerLoginInfo> BuyerLoginList { get; set; } = new ObservableCollectionThreadSafe<BuyerLoginInfo>();
public int InvoiceStatus
{
get => GetValue<int>();
set => SetValue(value, InvoiceStatusChanged);
}
private void InvoiceStatusChanged()
{
@@ -139,18 +157,30 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
{
WPFCEFBrowser.SetCookieAsync(SelectedBuyer.cookie, Browser.GetCookieManager()).GetAwaiter();
WPFCEFBrowser.SetCookieAsync(SelectedBuyer.cookie, BrowserDetail.GetCookieManager()).GetAwaiter();
}
/// <summary> 订单列表 </summary>
public ObservableCollection<PddBrowserOrderInvoiceViewModel> Orders { get; }
public ICollectionView OrdersView { get; }
/// <summary> 搜索关键字:订单号 / 商品名称 / 买家 </summary>
public string SearchText
{
get => _searchText;
set => SetProperty(ref _searchText, value, nameof(SearchText));
}
public int InvoiceStatus
{
get => GetValue<int>();
set => SetValue(value, InvoiceStatusChanged);
}
public DateTime StartTime
{
get => GetValue<DateTime>();
set => SetValue(value);
}
public DateTime EndTime
{
get => GetValue<DateTime>();
set => SetValue(value);
}
/// <summary> 开票弹窗是否打开 </summary>
public bool IsOrderListDialogOpen
@@ -184,19 +214,19 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
set => SetProperty(ref _selectedOrder, value, nameof(SelectedOrder));
}
/// <summary> 发票抬头 </summary>
public string InvoiceTitle
{
get => _invoiceTitle;
set => SetProperty(ref _invoiceTitle, value, nameof(InvoiceTitle));
}
///// <summary> 发票抬头 </summary>
//public string InvoiceTitle
//{
// get => _invoiceTitle;
// set => SetProperty(ref _invoiceTitle, value, nameof(InvoiceTitle));
//}
/// <summary> 税号 </summary>
public string TaxNo
{
get => _taxNo;
set => SetProperty(ref _taxNo, value, nameof(TaxNo));
}
///// <summary> 税号 </summary>
//public string TaxNo
//{
// get => _taxNo;
// set => SetProperty(ref _taxNo, value, nameof(TaxNo));
//}
/// <summary>
/// 开始开票总金额
/// </summary>
@@ -205,14 +235,14 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
get => GetValue<decimal>();
set => SetValue(value);
}
/// <summary>
/// 最大开票金额
/// </summary>
public decimal MaxAmount
{
get => GetValue<decimal>();
set => SetValue(value);
}
///// <summary>
///// 最大开票金额
///// </summary>
//public decimal MaxAmount
//{
// get => GetValue<decimal>();
// set => SetValue(value);
//}
/// <summary>
/// 已开票金额
/// </summary>
@@ -229,6 +259,16 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
get => GetValue<decimal>();
set => SetValue(value);
}
public bool IsLoadImage
{
get => GetValue<bool>();
set => SetValue(value);
}
public ICommand SearchCommand => new DelegateCommand(Search);
public ICommand ResetCommand => new DelegateCommand(ResetSearch);
@@ -250,7 +290,6 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
BaseMsgWindow.ShowError("未填选择买手号");
return;
}
_cancellationTokenSource = new CancellationTokenSource();
Debug.WriteLine("开始读单");
BottomMsg = $"开始读取订单";
StartProgress();
@@ -259,12 +298,34 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
if (!IsReadOrder)
{
_orderSns.Clear();
_allOrders.Clear();
Orders.Clear();
OrderInvoiceInfoList.Clear();
string path = string.Format(inviceDataPath, SelectedBuyer.buyer_id);
if (File.Exists(NaiBa.Util.Helper.ConfigHelper.GetConfigUserPath(path)))
{
var ret = BaseMsgWindow.ShowConfirm("检测到已有历史数据是否加载?").DialogResult;
if (ret == true)
{
var data = NaiBa.Util.Helper.ConfigHelper.LoadConfig<List<OrderInvoiceInfo>>(path);
AddOrders(data.Select(a => new PddBrowserOrderInvoiceViewModel(a)));
BottomMsg = $"已加载历史数据";
return;
}
else if (ret == null)
{
return;
}
}
Browser.Load("https://mobile.yangkeduo.com/orders.html");
await Task.Delay(Timeout.Infinite, _cancellationTokenSource.Token);
}
await Task.Delay(Timeout.Infinite, _cancellationTokenSource.Token);
}
catch (TaskCanceledException)
@@ -373,36 +434,50 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
{
if (IsBatchInvoicing) return;
if (string.IsNullOrEmpty(InvoiceTitle) || string.IsNullOrEmpty(TaxNo))
if (string.IsNullOrEmpty(Config.Title) || string.IsNullOrEmpty(Config.TaxNo))
{
BaseMsgWindow.ShowError("未填写公司或税号");
return;
}
var targets = Orders.Where(o => o.IsSelected && !o.IsInvoiced).ToList();
var targets = OrdersView.Cast<PddBrowserOrderInvoiceViewModel>().Where(o => o.IsSelected && !o.IsInvoiced).ToList();
if (targets.Count == 0)
{
BaseMsgWindow.ShowError("请先勾选需要开票的订单");
return;
}
if (MaxAmount <= 0)
if (Config.MaxAmount <= 0)
{
BaseMsgWindow.ShowError("请填写开票总金额");
return;
}
var confirm = BaseMsgWindow.ShowConfirm(
$"已勾选 {targets.Count} 个待开票订单,确定开始批量开票吗?\n请确认发票抬头与税号无误。\n开票总金额:{MaxAmount}",
$"已勾选 {targets.Count} 个待开票订单,确定开始批量开票吗?\n请确认发票抬头与税号无误。\n开票总金额:{Config.MaxAmount}",
"批量开票");
if (confirm.DialogResult != true) return;
Config.Title = InvoiceTitle;
Config.TaxNo = TaxNo;
Config.MaxAmount = MaxAmount;
NaiBa.Util.Helper.ConfigHelper.SaveConfig(ConfigPath, Config);
//Config.Title = InvoiceTitle;
//Config.TaxNo = TaxNo;
//Config.MaxAmount = MaxAmount;
//NaiBa.Util.Helper.ConfigHelper.SaveConfig(ConfigPath, Config);
await BatchInvoiceAsync(targets);
}
public ICommand ShowSettingDialogCommand => new DelegateCommand(ShowSettingDialog);
private void ShowSettingDialog()
{
var view = new PDDInvoiceDialog();
new DialogWindow
{
Title = "设置",
Content = view,
}.ShowDialog();
Config = view.Config;
}
public event EventHandler<int> BatchInvoiceChanged;
/// <summary>
/// 后台批量开票:逐单加载开票页并自动填写提交,底部进度条实时更新,成功后更新列表状态
/// </summary>
@@ -419,13 +494,15 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
for (int i = 0; i < targets.Count; i++)
{
if (AmountTotal >= MaxAmount)
if (AmountTotal >= Config.MaxAmount)
{
return;
}
var order = targets[i];
BottomMsg = $"正在批量开票:{i}/{targets.Count},当前订单:{order.order_sn}";
ProgressValue = i;
SelectedOrder = order;
BatchInvoiceChanged?.Invoke(this, OrdersView.Cast<PddBrowserOrderInvoiceViewModel>().ToList().IndexOf(order));
bool ok = false;
if (EnvironmentHelper.IsTest)
{
@@ -454,6 +531,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
}
ProgressValue = success + fail;
BottomMsg = $"正在批量开票:{i}/{targets.Count},当前订单:{order.order_sn}。总金额:{AmountTotal.ToString("N2")}";
BottomRMsg = $"批量开票状态:成功 {success} 个,失败 {fail} 个。总金额:{AmountTotal.ToString("N2")}";
StatisticsInvoiceAmount();
await Task.Delay(1000);
}
@@ -468,7 +546,6 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
_currentInvoiceTcs = null;
ProgressMaximum = targets.Count;
ProgressValue = targets.Count;
BottomMsg = $"批量开票完成:成功 {success} 个,失败 {fail} 个。总金额:{AmountTotal.ToString("N2")}";
IsBatchInvoicing = false;
BaseMsgWindow.ShowInfo(BottomMsg);
}
@@ -478,8 +555,8 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
/// </summary>
private void StatisticsInvoiceAmount()
{
InvoicedAmountTotal = Orders.Where(a => a.IsInvoiced).Sum(a => a.order_amount) / 100m;
UninvoicedAmountTotal = Orders.Where(a => a.IsInvoiced == false).Sum(a => a.order_amount) / 100m;
InvoicedAmountTotal = OrdersView.Cast<PddBrowserOrderInvoiceViewModel>().Where(a => a.IsInvoiced).Sum(a => a.order_amount) / 100m;
UninvoicedAmountTotal = OrdersView.Cast<PddBrowserOrderInvoiceViewModel>().Where(a => a.IsInvoiced == false).Sum(a => a.order_amount) / 100m;
}
#endregion
@@ -487,21 +564,22 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
/// <summary> 按 订单号 / 商品名称 过滤订单 </summary>
private void Search()
{
var keyword = SearchText?.Trim();
Orders.Clear();
bool? _tempIsInvoiced = InvoiceStatus == 0 ? null : InvoiceStatus == 2;
foreach (var order in _allOrders)
{
if (_tempIsInvoiced.HasValue)
{
if (order.IsInvoiced != _tempIsInvoiced.Value) continue;
}
if (!IsKeywordMatch(order, keyword))
{
continue;
}
Orders.Add(order);
}
OrdersView.Refresh();
StatisticsInvoiceAmount();
//var keyword = SearchText?.Trim();
//bool? _tempIsInvoiced = InvoiceStatus == 0 ? null : InvoiceStatus == 2;
//foreach (var order in Orders)
//{
// if (_tempIsInvoiced.HasValue)
// {
// if (order.IsInvoiced != _tempIsInvoiced.Value) continue;
// }
// if (!IsKeywordMatch(order, keyword))
// {
// continue;
// }
// Orders.Add(order);
//}
}
private static bool IsKeywordMatch(PddBrowserOrderInvoiceViewModel order, string keyword)
@@ -515,6 +593,8 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
{
SearchText = "";
InvoiceStatus = 0;
EndTime = DateTime.Now;
StartTime = EndTime.AddMonths(-1);
Search();
}
/// <summary>
@@ -524,7 +604,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
private void OpenInvoice(PddBrowserOrderInvoiceViewModel order)
{
SelectedOrder = order;
if (string.IsNullOrEmpty(InvoiceTitle) || string.IsNullOrEmpty(TaxNo))
if (string.IsNullOrEmpty(Config.Title) || string.IsNullOrEmpty(Config.TaxNo))
{
BaseMsgWindow.ShowError("未填写公司或税号");
return;
@@ -602,15 +682,30 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
{
continue;
}
_allOrders.Add(item);
if (IsKeywordMatch(item, keyword))
if (item.IsInvoiced == false)
{
item.IsInvoiced = item.order_buttons?.Any(a => a.brief_prompt == "发票详情") ?? false;
Orders.Add(item);
}
Orders.Add(item);
if (Config.IsSaveData)
{
OrderInvoiceInfoList.Add(new OrderInvoiceInfo
{
GoodsName = item.GoodsName,
order_amount = item.order_amount,
order_time = item.order_time,
order_sn = item.order_sn,
order_link_url = item.order_link_url,
IsInvoiced = item.IsInvoiced,
ThumbUrl = item.ThumbUrl
});
}
}
StatisticsInvoiceAmount();
SaveOrderInvoiceInfo();
}
#endregion
@@ -651,7 +746,29 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
return Task.CompletedTask;
}
private const string inviceDataPath = "inviceData_{0}";
/// <summary>
/// 保存数据
/// </summary>
private void SaveOrderInvoiceInfo()
{
if (SelectedBuyer == null) return;
string path = string.Format(inviceDataPath, SelectedBuyer.buyer_id);
NaiBa.Util.Helper.ConfigHelper.SaveConfig<List<OrderInvoiceInfo>>(path, OrderInvoiceInfoList);
}
private void GetOrderInvoiceInfo()
{
if (SelectedBuyer == null) return;
string path = string.Format(inviceDataPath, SelectedBuyer.buyer_id);
if (File.Exists(NaiBa.Util.Helper.ConfigHelper.GetConfigUserPath(path)))
{
if (BaseMsgWindow.ShowConfirm("检测到已有旧数据是否加载?").DialogResult == true)
{
var data = NaiBa.Util.Helper.ConfigHelper.LoadConfig<List<OrderInvoiceInfo>>(path);
AddOrders(data.Select(a => new PddBrowserOrderInvoiceViewModel(a)));
}
}
}
@@ -664,6 +781,13 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
set => SetValue(ref _bottomMsg, value);
}
private string _bottomRMsg = "";
public string BottomRMsg
{
get => _bottomRMsg;
set => SetValue(ref _bottomRMsg, value);
}
private double _progressValue;
public double ProgressValue
{
@@ -754,9 +878,10 @@ public partial class PDDInvoiceViewModel : BindableBaseEx
#endregion
}
class InvoiceConfig
public class InvoiceConfig
{
public string Title { get; set; }
public string TaxNo { get; set; }
public decimal MaxAmount { get; set; }
public bool IsSaveData { get; set; }
}