-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAssetShell.cs
313 lines (280 loc) · 12.1 KB
/
AssetShell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 2020-2024 b1f6c1c4
*
* This file is part of ProfessionalAccounting.
*
* ProfessionalAccounting is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3.
*
* ProfessionalAccounting is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ProfessionalAccounting. If not, see
* <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AccountingServer.BLL;
using AccountingServer.BLL.Util;
using AccountingServer.Entities;
using AccountingServer.Entities.Util;
using AccountingServer.Shell.Serializer;
using static AccountingServer.BLL.Parsing.FacadeF;
namespace AccountingServer.Shell;
/// <summary>
/// 资产表达式解释器
/// </summary>
internal class AssetShell : DistributedShell
{
/// <inheritdoc />
protected override string Initial => "a";
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteList(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateTime? dt,
bool showSchedule, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
yield return await ListAsset(a, session, dt, showSchedule);
}
/// <inheritdoc />
protected override IAsyncEnumerable<string> ExecuteQuery(IQueryCompounded<IDistributedQueryAtom> distQuery,
Session session)
=> session.Serializer.PresentAssets(Sort(session.Accountant.SelectAssetsAsync(distQuery)));
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteRegister(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateFilter rng,
IQueryCompounded<IVoucherQueryAtom> query, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
{
await foreach (var s in session.Serializer.PresentVouchers(
session.Accountant.RegisterVouchers(a, rng, query).ToAsyncEnumerable()))
yield return s;
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteUnregister(
IQueryCompounded<IDistributedQueryAtom> distQuery, DateFilter rng,
IQueryCompounded<IVoucherQueryAtom> query, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
{
foreach (var item in a.Schedule.Where(item => item.Date.Within(rng)))
{
if (query != null)
{
if (item.VoucherID == null)
continue;
var voucher = await session.Accountant.SelectVoucherAsync(item.VoucherID);
if (voucher != null)
if (!MatchHelper.IsMatch(query, voucher.IsMatch))
continue;
}
item.VoucherID = null;
}
yield return await ListAsset(a, session);
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteRecal(IQueryCompounded<IDistributedQueryAtom> distQuery,
Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
{
Accountant.Depreciate(a);
yield return session.Serializer.PresentAsset(a);
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteResetSoft(
IQueryCompounded<IDistributedQueryAtom> distQuery, DateFilter rng, Session session)
{
await foreach (var a in session.Accountant.SelectAssetsAsync(distQuery))
{
if (a.Schedule == null)
continue;
var flag = false;
foreach (var item in a.Schedule.Where(item => item.Date.Within(rng))
.Where(static item => item.VoucherID != null))
{
if (await session.Accountant.SelectVoucherAsync(item.VoucherID) != null)
continue;
item.VoucherID = null;
flag = true;
}
if (!flag)
continue;
yield return session.Serializer.PresentAsset(a);
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteResetMixed(
IQueryCompounded<IDistributedQueryAtom> distQuery, DateFilter rng, Session session)
{
await foreach (var a in session.Accountant.SelectAssetsAsync(distQuery))
{
if (a.Schedule == null)
continue;
var flag = false;
foreach (var item in a.Schedule.Where(item => item.Date.Within(rng))
.Where(static item => item.VoucherID != null))
{
var voucher = await session.Accountant.SelectVoucherAsync(item.VoucherID);
if (voucher == null)
{
item.VoucherID = null;
flag = true;
}
else if (await session.Accountant.DeleteVoucherAsync(voucher.ID))
{
item.VoucherID = null;
flag = true;
}
}
if (!flag)
continue;
yield return session.Serializer.PresentAsset(a);
await session.Accountant.UpsertAsync(a);
}
}
protected override async IAsyncEnumerable<string> ExecuteResetHard(
IQueryCompounded<IDistributedQueryAtom> distQuery, IQueryCompounded<IVoucherQueryAtom> query, Session session)
{
await foreach (var a in session.Accountant.SelectAssetsAsync(distQuery))
{
var cnt = await session.Accountant.DeleteVouchersAsync(
new IntersectQueries<IVoucherQueryAtom>(
query ?? VoucherQueryUnconstrained.Instance,
ParsingF.VoucherQuery(
$"{{ {a.DepreciationTitle.AsTitle()} {a.StringID.Quotation('\'')} Depreciation }} + {{ {a.DevaluationTitle.AsTitle()} {a.StringID.Quotation('\'')} Devalue }}",
session.Client)));
yield return $"{a.StringID} {a.Name} => {cnt}\n";
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteApply(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateFilter rng, bool isCollapsed, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
{
await foreach (var item in session.Accountant.Update(a, rng, isCollapsed))
yield return ListAssetItem(item);
await session.Accountant.UpsertAsync(a);
}
}
/// <summary>
/// 执行检查表达式
/// </summary>
/// <param name="distQuery">分期检索式</param>
/// <param name="rng">日期过滤器</param>
/// <param name="session">客户端会话</param>
/// <returns>执行结果</returns>
protected override async IAsyncEnumerable<string> ExecuteCheck(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateFilter rng, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAssetsAsync(distQuery)))
{
var sbi = new StringBuilder();
await foreach (var item in session.Accountant.Update(a, rng, false, true))
sbi.Append(ListAssetItem(item));
if (sbi.Length != 0)
{
yield return await ListAsset(a, session, null, false);
yield return sbi.ToString();
}
await session.Accountant.UpsertAsync(a);
}
}
/// <summary>
/// 显示资产及其折旧计算表
/// </summary>
/// <param name="asset">资产</param>
/// <param name="session">客户端会话</param>
/// <param name="dt">计算账面价值的时间</param>
/// <param name="showSchedule">是否显示折旧计算表</param>
/// <returns>格式化的信息</returns>
private async ValueTask<string> ListAsset(Asset asset, Session session, DateTime? dt = null,
bool showSchedule = true)
{
var sb = new StringBuilder();
var bookValue = Accountant.GetBookValueOn(asset, dt);
if (dt.HasValue &&
!bookValue?.IsZero() != true)
return null;
sb.Append($"{asset.StringID} {asset.Name.CPadRight(35)}{asset.Date:yyyyMMdd}" +
$"{asset.User.AsUser().CPadRight(6)} " +
asset.Value.AsFund(asset.Currency).CPadLeft(13) +
(dt.HasValue ? bookValue.AsFund(asset.Currency).CPadLeft(13) : "-".CPadLeft(13)) +
asset.Salvage.AsFund(asset.Currency).CPadLeft(13) +
asset.Title.AsTitle().CPadLeft(6) +
asset.DepreciationTitle.AsTitle().CPadLeft(6) +
asset.DevaluationTitle.AsTitle().CPadLeft(6) +
asset.DepreciationExpenseTitle.AsTitle().CPadLeft(6) +
asset.DepreciationExpenseSubTitle.AsSubTitle() +
asset.DevaluationExpenseTitle.AsTitle().CPadLeft(6) +
asset.DevaluationExpenseSubTitle.AsSubTitle() +
asset.Life.ToString().CPadLeft(4) +
asset.Method.ToString().CPadLeft(20) + "\n");
if (showSchedule && asset.Schedule != null)
foreach (var assetItem in asset.Schedule)
{
sb.Append(ListAssetItem(assetItem));
if (assetItem.VoucherID != null)
sb.Append(session.Serializer
.PresentVoucher(await session.Accountant.SelectVoucherAsync(assetItem.VoucherID)).Wrap());
}
return sb.ToString();
}
/// <summary>
/// 显示折旧计算表条目
/// </summary>
/// <param name="assetItem">折旧计算表条目</param>
/// <returns>格式化的信息</returns>
private static string ListAssetItem(IDistributedItem assetItem)
=> assetItem switch
{
AcquisitionItem acq => string.Format(
" {0:yyyMMdd} ACQ:{1} ={3} ({2})\n",
assetItem.Date,
acq.OrigValue.AsFund().CPadLeft(13),
assetItem.VoucherID,
assetItem.Value.AsFund().CPadLeft(13)),
DepreciateItem dep => string.Format(
" {0:yyyMMdd} DEP:{1} ={3} ({2})\n",
assetItem.Date,
dep.Amount.AsFund().CPadLeft(13),
assetItem.VoucherID,
assetItem.Value.AsFund().CPadLeft(13)),
DevalueItem dev => string.Format(
" {0:yyyMMdd} DEV:{1} ={3} ({2})\n",
assetItem.Date,
dev.Amount.AsFund().CPadLeft(13),
assetItem.VoucherID,
assetItem.Value.AsFund().CPadLeft(13)),
DispositionItem => string.Format(
" {0:yyyMMdd} DSP:{1} ={3} ({2})\n",
assetItem.Date,
"ALL".CPadLeft(13),
assetItem.VoucherID,
assetItem.Value.AsFund().CPadLeft(13)),
_ => null,
};
/// <summary>
/// 对资产进行排序
/// </summary>
/// <param name="enumerable">资产</param>
/// <returns>排序后的资产</returns>
private static IAsyncEnumerable<Asset> Sort(IAsyncEnumerable<Asset> enumerable)
=> enumerable.OrderBy(static a => a.Date, new DateComparer()).ThenBy(static a => a.Name)
.ThenBy(static a => a.ID);
}