IncomingInvoiceHandlers.cs
9.93 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Contracts.IncomingInvoice;
using Sungero.Core;
using Sungero.CoreEntities;
namespace Sungero.Contracts
{
partial class IncomingInvoiceConvertingFromServerHandler
{
public override void ConvertingFrom(Sungero.Domain.ConvertingFromEventArgs e)
{
base.ConvertingFrom(e);
if (Sungero.Docflow.AccountingDocumentBases.Is(_source))
{
e.Map(_info.Properties.Number, Sungero.Docflow.AccountingDocumentBases.Info.Properties.RegistrationNumber);
e.Map(_info.Properties.Date, Sungero.Docflow.AccountingDocumentBases.Info.Properties.RegistrationDate);
e.Map(_info.Properties.Contract, Sungero.Docflow.AccountingDocumentBases.Info.Properties.LeadingDocument);
// Котегов: Отключен проброс Number и Date, иначе они перетирали одноименные свойства (баг 115832).
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.Number);
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.Date);
// Отключить проброс полей, которых нет во входящих счетах.
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.IsAdjustment);
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.CounterpartySignatory);
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.CounterpartySigningReason);
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.Contact);
e.Without(Sungero.Docflow.AccountingDocumentBases.Info.Properties.ResponsibleEmployee);
}
// Исключаем проброс LeadingDocument, так как в счете должно заполняться поле Contract.
e.Without(Sungero.Docflow.OfficialDocuments.Info.Properties.LeadingDocument);
// При смене типа с вх. документа эл. обмена, а также с финансовых и договорных документов
// дополнить примечание информацией об основании подписания со стороны контрагента.
var sourceOfficialDocument = Sungero.Docflow.OfficialDocuments.As(_source);
if (sourceOfficialDocument != null)
{
var note = Sungero.Docflow.PublicFunctions.OfficialDocument.GetNoteWithCounterpartySigningReason(sourceOfficialDocument);
e.Map(_info.Properties.Note, note);
}
}
}
partial class IncomingInvoiceCreatingFromServerHandler
{
public override void CreatingFrom(Sungero.Domain.CreatingFromEventArgs e)
{
base.CreatingFrom(e);
if (_source.Contract == null || !_source.Contract.AccessRights.CanRead())
e.Without(_info.Properties.Contract);
}
}
partial class IncomingInvoiceServerHandlers
{
public override void BeforeSaveHistory(Sungero.Content.DocumentHistoryEventArgs e)
{
var isCreateAction = e.Action == Sungero.CoreEntities.History.Action.Create;
var isChangeTypeAction = e.Action == Sungero.CoreEntities.History.Action.ChangeType;
if (!isCreateAction && !isChangeTypeAction)
base.BeforeSaveHistory(e);
else
{
// Изменение суммы или валюты.
var sumWasChanged = _obj.State.Properties.TotalAmount.IsChanged || (_obj.State.Properties.Currency.IsChanged && _obj.TotalAmount.HasValue);
if (sumWasChanged)
{
// Локализация для операции в ресурсах OfficialDocument.
var operation = new Enumeration(Sungero.Docflow.Constants.OfficialDocument.Operation.TotalAmountChange);
var operationDetailed = _obj.TotalAmount.HasValue ? operation : new Enumeration(Sungero.Docflow.Constants.OfficialDocument.Operation.TotalAmountClear);
var currency = (_obj.Currency == null) ? string.Empty : _obj.Currency.AlphaCode;
var comment = _obj.TotalAmount.HasValue ? string.Join("|", _obj.TotalAmount.Value, currency) : string.Empty;
e.Write(operation, operationDetailed, comment);
}
var documentParams = ((Domain.Shared.IExtendedEntity)_obj).Params;
if (isCreateAction && documentParams.ContainsKey(Docflow.PublicConstants.OfficialDocument.AddHistoryCommentRepackingAddNewDocument))
e.Comment = Sungero.Docflow.OfficialDocuments.Resources.DocumentCreateFromRepacking;
}
}
public override void BeforeSave(Sungero.Domain.BeforeSaveEventArgs e)
{
base.BeforeSave(e);
if (_obj.Date.HasValue && _obj.PaymentDueDate.HasValue &&
_obj.Date.Value > _obj.PaymentDueDate)
e.AddError(_obj.Info.Properties.PaymentDueDate, IncomingInvoices.Resources.DatePaymentDeadlineValidationMessage, _obj.Info.Properties.Date);
if (Functions.IncomingInvoice.HaveDuplicates(_obj,
_obj.DocumentKind,
_obj.Number,
_obj.Date,
_obj.TotalAmount,
_obj.Currency,
_obj.Counterparty))
e.AddWarning(IncomingInvoices.Resources.DuplicateDetected, _obj.Info.Actions.ShowDuplicates);
if (_obj.Contract != null && _obj.Contract.AccessRights.CanRead() && !_obj.Relations.GetRelatedFrom(Constants.Module.AccountingDocumentsRelationName).Contains(_obj.Contract))
_obj.Relations.AddFromOrUpdate(Constants.Module.AccountingDocumentsRelationName, _obj.State.Properties.Contract.OriginalValue, _obj.Contract);
_obj.DocumentDate = _obj.Date.HasValue ? _obj.Date : _obj.Created;
}
public override void Created(Sungero.Domain.CreatedEventArgs e)
{
base.Created(e);
if (_obj.State.IsInserted && _obj.Contract != null)
_obj.Relations.AddFrom(Constants.Module.AccountingDocumentsRelationName, _obj.Contract);
_obj.ResponsibleEmployee = null;
}
}
partial class IncomingInvoiceFilteringServerHandler<T>
{
/// <summary>
/// Фильтрация списка входящих счетов.
/// </summary>
/// <param name="query">Фильтруемый список счетов.</param>
/// <param name="e">Аргументы события фильтрации.</param>
/// <returns>Список счетов с примененными фильтрами.</returns>
public override IQueryable<T> Filtering(IQueryable<T> query, Sungero.Domain.FilteringEventArgs e)
{
if (_filter == null)
return base.Filtering(query, e);
// Состояние.
if ((_filter.Draft || _filter.OnApproval || _filter.PayAccepted || _filter.PayRejected || _filter.PayComplete) &&
!(_filter.Draft && _filter.OnApproval && _filter.PayAccepted && _filter.PayRejected && _filter.PayComplete))
{
query = query.Where(x => (_filter.Draft && x.LifeCycleState == IncomingInvoice.LifeCycleState.Draft && x.InternalApprovalState == null) ||
(_filter.OnApproval && x.LifeCycleState != IncomingInvoice.LifeCycleState.Obsolete &&
(x.InternalApprovalState == Docflow.OfficialDocument.InternalApprovalState.PendingSign ||
x.InternalApprovalState == Docflow.OfficialDocument.InternalApprovalState.OnRework ||
x.InternalApprovalState == Docflow.OfficialDocument.InternalApprovalState.OnApproval)) ||
(_filter.PayAccepted && x.LifeCycleState == IncomingInvoice.LifeCycleState.Active) ||
(_filter.PayRejected && x.LifeCycleState == IncomingInvoice.LifeCycleState.Obsolete) ||
(_filter.PayComplete && x.LifeCycleState == IncomingInvoice.LifeCycleState.Paid));
}
// Контрагент.
if (_filter.Counterparty != null)
query = query.Where(x => Equals(x.Counterparty, _filter.Counterparty));
// НОР.
if (_filter.BusinessUnit != null)
query = query.Where(x => Equals(x.BusinessUnit, _filter.BusinessUnit));
// Подразделение.
if (_filter.Department != null)
query = query.Where(x => Equals(x.Department, _filter.Department));
// Дата.
var beginDate = Calendar.UserToday.AddDays(-30);
var endDate = Calendar.UserToday;
if (_filter.Last7daysInvoice)
beginDate = Calendar.UserToday.AddDays(-7);
if (_filter.ManualPeriodInvoice)
{
beginDate = _filter.DateRangeInvoiceFrom ?? Calendar.SqlMinValue;
endDate = _filter.DateRangeInvoiceTo ?? Calendar.SqlMaxValue;
}
var serverPeriodBegin = Equals(Calendar.SqlMinValue, beginDate) ? beginDate : Docflow.PublicFunctions.Module.Remote.GetTenantDateTimeFromUserDay(beginDate);
var serverPeriodEnd = Equals(Calendar.SqlMaxValue, endDate) ? endDate : endDate.EndOfDay().FromUserTime();
var clientPeriodEnd = !Equals(Calendar.SqlMaxValue, endDate) ? endDate.AddDays(1) : Calendar.SqlMaxValue;
query = query.Where(j => (j.DocumentDate.Between(serverPeriodBegin, serverPeriodEnd) ||
j.DocumentDate == beginDate) && j.DocumentDate != clientPeriodEnd);
return query;
}
}
partial class IncomingInvoiceContractPropertyFilteringServerHandler<T>
{
public virtual IQueryable<T> ContractFiltering(IQueryable<T> query, Sungero.Domain.PropertyFilteringEventArgs e)
{
if (_obj.Counterparty != null)
query = query.Where(c => Equals(c.Counterparty, _obj.Counterparty));
query = query.Where(c => !Equals(c.LifeCycleState, Sungero.Contracts.ContractBase.LifeCycleState.Obsolete));
return query;
}
}
}