-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAmortizationShell.cs
271 lines (239 loc) · 10.1 KB
/
AmortizationShell.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
/* 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.Globalization;
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;
namespace AccountingServer.Shell;
/// <summary>
/// 摊销表达式解释器
/// </summary>
internal class AmortizationShell : DistributedShell
{
/// <inheritdoc />
protected override string Initial => "o";
/// <summary>
/// 执行列表表达式
/// </summary>
/// <param name="distQuery">分期检索式</param>
/// <param name="dt">计算账面价值的时间</param>
/// <param name="showSchedule">是否显示折旧计算表</param>
/// <param name="session">客户端会话</param>
/// <returns>执行结果</returns>
protected override IAsyncEnumerable<string> ExecuteList(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateTime? dt, bool showSchedule, Session session)
=> Sort(session.Accountant.SelectAmortizationsAsync(distQuery))
.SelectAwait(a => ListAmort(a, session, dt, showSchedule));
/// <inheritdoc />
protected override IAsyncEnumerable<string> ExecuteQuery(IQueryCompounded<IDistributedQueryAtom> distQuery,
Session session)
=> session.Serializer.PresentAmorts(Sort(session.Accountant.SelectAmortizationsAsync(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.SelectAmortizationsAsync(distQuery)))
{
await foreach (var s in session.Serializer.PresentVouchers(
session.Accountant.RegisterVouchers(a, rng, query)))
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.SelectAmortizationsAsync(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 ListAmort(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.SelectAmortizationsAsync(distQuery)))
{
Accountant.Amortize(a);
yield return session.Serializer.PresentAmort(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.SelectAmortizationsAsync(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.PresentAmort(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.SelectAmortizationsAsync(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.PresentAmort(a);
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override IAsyncEnumerable<string> ExecuteResetHard(IQueryCompounded<IDistributedQueryAtom> distQuery,
IQueryCompounded<IVoucherQueryAtom> query, Session session) => throw new InvalidOperationException();
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteApply(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateFilter rng,
bool isCollapsed, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAmortizationsAsync(distQuery)))
{
await foreach (var item in session.Accountant.Update(a, rng, isCollapsed))
yield return ListAmortItem(item);
await session.Accountant.UpsertAsync(a);
}
}
/// <inheritdoc />
protected override async IAsyncEnumerable<string> ExecuteCheck(IQueryCompounded<IDistributedQueryAtom> distQuery,
DateFilter rng, Session session)
{
await foreach (var a in Sort(session.Accountant.SelectAmortizationsAsync(distQuery)))
{
var sbi = new StringBuilder();
await foreach (var item in session.Accountant.Update(a, rng, false, true))
sbi.Append(ListAmortItem(item));
if (sbi.Length != 0)
{
yield return await ListAmort(a, session, null, false);
yield return sbi.ToString();
}
await session.Accountant.UpsertAsync(a);
}
}
/// <summary>
/// 显示摊销及其计算表
/// </summary>
/// <param name="amort">摊销</param>
/// <param name="session">客户端会话</param>
/// <param name="dt">计算账面价值的时间</param>
/// <param name="showSchedule">是否显示计算表</param>
/// <returns>格式化的信息</returns>
private async ValueTask<string> ListAmort(Amortization amort, Session session, DateTime? dt = null,
bool showSchedule = true)
{
var sb = new StringBuilder();
var bookValue = Accountant.GetBookValueOn(amort, dt);
if (dt.HasValue &&
!bookValue?.IsZero() != true)
return null;
sb.Append(
$"{amort.StringID} {amort.Name.CPadRight(35)}{amort.Date:yyyyMMdd}" +
$"{amort.User.AsUser().CPadRight(6)} " +
$"{amort.Value.AsFund().CPadLeft(13)}{(dt.HasValue ? bookValue.AsFund().CPadLeft(13) : "-".CPadLeft(13))}" +
$"{(amort.TotalDays?.ToString(CultureInfo.InvariantCulture) ?? "-").CPadLeft(4)}{amort.Interval.ToString().CPadLeft(20)}\n");
if (showSchedule && amort.Schedule != null)
foreach (var amortItem in amort.Schedule)
{
sb.Append(ListAmortItem(amortItem));
if (amortItem.VoucherID != null)
sb.Append(session.Serializer
.PresentVoucher(await session.Accountant.SelectVoucherAsync(amortItem.VoucherID)).Wrap());
}
return sb.ToString();
}
/// <summary>
/// 显示摊销计算表条目
/// </summary>
/// <param name="amortItem">摊销计算表条目</param>
/// <returns>格式化的信息</returns>
private static string ListAmortItem(AmortItem amortItem)
=> string.Format(
" {0:yyyMMdd} AMO:{1} ={3} ({2})\n",
amortItem.Date,
amortItem.Amount.AsFund()
.CPadLeft(13),
amortItem.VoucherID,
amortItem.Value.AsFund()
.CPadLeft(13));
/// <summary>
/// 对摊销进行排序
/// </summary>
/// <param name="enumerable">摊销</param>
/// <returns>排序后的摊销</returns>
private static IAsyncEnumerable<Amortization> Sort(IAsyncEnumerable<Amortization> enumerable)
=> enumerable.OrderBy(static o => o.Date, new DateComparer()).ThenBy(static o => o.Name)
.ThenBy(static o => o.ID);
}