-
Notifications
You must be signed in to change notification settings - Fork 600
/
APIV2CustomerPayments.Page.al
336 lines (292 loc) · 12.8 KB
/
APIV2CustomerPayments.Page.al
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
namespace Microsoft.API.V2;
using Microsoft.Finance.GeneralLedger.Journal;
using Microsoft.Sales.Customer;
using Microsoft.Integration.Entity;
using Microsoft.Sales.History;
using Microsoft.Integration.Graph;
page 30055 "APIV2 - Customer Payments"
{
APIVersion = 'v2.0';
EntityCaption = 'Customer Payment';
EntitySetCaption = 'Customer Payments';
DelayedInsert = true;
ODataKeyFields = SystemId;
PageType = API;
EntityName = 'customerPayment';
EntitySetName = 'customerPayments';
SourceTable = "Gen. Journal Line";
Extensible = false;
layout
{
area(content)
{
repeater(Group)
{
field(id; Rec.SystemId)
{
Caption = 'Id';
Editable = false;
}
field(journalId; Rec."Journal Batch Id")
{
Caption = 'Journal Id';
trigger OnValidate()
begin
if (not IsNullGuid(xRec."Journal Batch Id")) and (xRec."Journal Batch Id" <> Rec."Journal Batch Id") then
Error(CannotEditJournalIdErr);
end;
}
field(journalDisplayName; Rec."Journal Batch Name")
{
Caption = 'Journal Display Name';
trigger OnValidate()
begin
if (xRec."Journal Batch Name" <> '') and (xRec."Journal Batch Name" <> Rec."Journal Batch Name") then
Error(CannotEditBatchNameErr);
end;
}
field(lineNumber; Rec."Line No.")
{
Caption = 'Line No.';
}
field(customerId; Rec."Customer Id")
{
Caption = 'Customer Id';
trigger OnValidate()
begin
if Rec."Customer Id" = BlankGUID then begin
Rec."Account No." := '';
exit;
end;
if not Customer.GetBySystemId(Rec."Customer Id") then
Error(CustomerIdDoesNotMatchACustomerErr);
Rec."Account No." := Customer."No.";
end;
}
field(customerNumber; Rec."Account No.")
{
Caption = 'Customer No.';
TableRelation = Customer;
trigger OnValidate()
begin
if Customer."No." <> '' then begin
if Customer."No." <> Rec."Account No." then
Error(CustomerValuesDontMatchErr);
exit;
end;
if Rec."Account No." = '' then begin
Rec."Customer Id" := BlankGUID;
exit;
end;
if not Customer.Get(Rec."Account No.") then
Error(CustomerNumberDoesNotMatchACustomerErr);
Rec."Customer Id" := Customer.SystemId;
end;
}
field(postingDate; Rec."Posting Date")
{
Caption = 'Posting Date';
}
field(documentNumber; Rec."Document No.")
{
Caption = 'Document No.';
}
field(externalDocumentNumber; Rec."External Document No.")
{
Caption = 'External Document No.';
}
field(amount; Rec.Amount)
{
Caption = 'Amount';
}
field(appliesToInvoiceId; AppliesToInvoiceIdText)
{
Caption = 'Applies To Invoice Id';
trigger OnValidate()
var
SalesInvoiceAggregator: Codeunit "Sales Invoice Aggregator";
begin
Rec."Applies-to Invoice Id" := AppliesToInvoiceIdText;
if Rec."Applies-to Invoice Id" = BlankGUID then begin
AppliesToInvoiceNumberText := '';
exit;
end;
SalesInvoiceHeader.Reset();
if not SalesInvoiceAggregator.GetSalesInvoiceHeaderFromId(Format(AppliesToInvoiceIdText), SalesInvoiceHeader) then
Error(AppliesToInvoiceIdDoesNotMatchAnInvoiceErr);
AppliesToInvoiceNumberText := SalesInvoiceHeader."No.";
if Rec."Account No." = '' then
if SalesInvoiceHeader."Bill-to Customer No." <> '' then
Rec."Account No." := SalesInvoiceHeader."Bill-to Customer No."
else
Rec."Account No." := SalesInvoiceHeader."Sell-to Customer No.";
end;
}
field(appliesToInvoiceNumber; AppliesToInvoiceNumberText)
{
Caption = 'Applies To Invoice No.';
trigger OnValidate()
var
SalesInvoiceAggregator: Codeunit "Sales Invoice Aggregator";
BlankGUID: Guid;
begin
Rec."Applies-to Doc. No." := AppliesToInvoiceNumberText;
if SalesInvoiceHeader."No." <> '' then begin
if SalesInvoiceHeader."No." <> AppliesToInvoiceNumberText then
Error(AppliesToDocValuesDontMatchErr);
exit;
end;
if SalesInvoiceHeader.Get(AppliesToInvoiceNumberText) then begin
AppliesToInvoiceIdText := SalesInvoiceAggregator.GetSalesInvoiceHeaderId(SalesInvoiceHeader);
if Rec."Account No." = '' then
if SalesInvoiceHeader."Bill-to Customer No." <> '' then
Rec."Account No." := SalesInvoiceHeader."Bill-to Customer No."
else
Rec."Account No." := SalesInvoiceHeader."Sell-to Customer No.";
end else
AppliesToInvoiceIdText := BlankGUID;
end;
}
field(description; Rec.Description)
{
Caption = 'Description';
}
field(comment; Rec.Comment)
{
Caption = 'Comment';
}
field(lastModifiedDateTime; Rec.SystemModifiedAt)
{
Caption = 'Last Modified Date';
Editable = false;
}
part(dimensionSetLines; "APIV2 - Dimension Set Lines")
{
Caption = 'Dimension Set Lines';
EntityName = 'dimensionSetLine';
EntitySetName = 'dimensionSetLines';
SubPageLink = "Parent Id" = field(SystemId), "Parent Type" = const("Journal Line");
}
}
}
}
actions
{
}
trigger OnAfterGetCurrRecord()
begin
if not FiltersChecked then begin
CheckFilters();
FiltersChecked := true;
end;
end;
trigger OnAfterGetRecord()
begin
SetCalculatedFields();
end;
trigger OnInsertRecord(BelowxRec: Boolean): Boolean
var
TempGenJournalLine: Record "Gen. Journal Line" temporary;
JournalBatchId: Guid;
JournalBatchIdFilter: Text;
begin
if IsNullGuid(Rec."Journal Batch Id") then begin
JournalBatchIdFilter := Rec.GetFilter("Journal Batch Id");
if JournalBatchIdFilter = '' then
Error(FiltersNotSpecifiedErr);
JournalBatchId := JournalBatchIdFilter;
end else begin
JournalBatchIdFilter := Rec.GetFilter("Journal Batch Id");
if (JournalBatchIdFilter <> '') then begin
JournalBatchId := JournalBatchIdFilter;
if (JournalBatchId <> Rec."Journal Batch Id") then
Error(JournalBatchIdNameNotMatchErr)
end else
JournalBatchId := Rec."Journal Batch Id";
end;
ProcessAppliesToInvoiceNumberAndId();
TempGenJournalLine.Reset();
TempGenJournalLine.Copy(Rec);
Clear(Rec);
GraphMgtCustomerPayments.SetCustomerPaymentsTemplateAndBatch(
Rec, LibraryAPIGeneralJournal.GetBatchNameFromId(JournalBatchId));
LibraryAPIGeneralJournal.InitializeLine(
Rec, TempGenJournalLine."Line No.", TempGenJournalLine."Document No.", TempGenJournalLine."External Document No.");
TransferGeneratedFieldsFromInitializeLine(TempGenJournalLine);
GraphMgtCustomerPayments.SetCustomerPaymentsValues(Rec, TempGenJournalLine);
SetCalculatedFields();
end;
trigger OnModifyRecord(): Boolean
var
GenJournalLine: Record "Gen. Journal Line";
begin
ProcessAppliesToInvoiceNumberAndId();
GenJournalLine.GetBySystemId(Rec.SystemId);
if Rec."Line No." = GenJournalLine."Line No." then
Rec.Modify(true)
else begin
GenJournalLine.TransferFields(Rec, false);
GenJournalLine.Rename(Rec."Journal Template Name", Rec."Journal Batch Name", Rec."Line No.");
Rec.TransferFields(GenJournalLine, true);
end;
SetCalculatedFields();
exit(false);
end;
trigger OnNewRecord(BelowxRec: Boolean)
begin
ClearCalculatedFields();
Rec."Document Type" := Rec."Document Type"::Payment;
Rec."Account Type" := Rec."Account Type"::Customer;
Rec."Applies-to Doc. Type" := Rec."Applies-to Doc. Type"::Invoice;
end;
trigger OnOpenPage()
begin
GraphMgtCustomerPayments.SetCustomerPaymentsFilters(Rec);
end;
var
Customer: Record Customer;
SalesInvoiceHeader: Record "Sales Invoice Header";
GraphMgtCustomerPayments: Codeunit "Graph Mgt - Customer Payments";
LibraryAPIGeneralJournal: Codeunit "Library API - General Journal";
AppliesToInvoiceNumberText: Code[20];
AppliesToInvoiceIdText: Guid;
FiltersNotSpecifiedErr: Label 'You must specify a journal batch ID or a journal ID to get a journal line.';
JournalBatchIdNameNotMatchErr: Label 'The Journal Id and Journal Display Name do not match.';
CannotEditBatchNameErr: Label 'The Journal Batch Display Name cannot be changed.';
CannotEditJournalIdErr: Label 'The Journal Id cannot be changed.';
CustomerValuesDontMatchErr: Label 'The customer values do not match to a specific Customer.';
CustomerIdDoesNotMatchACustomerErr: Label 'The "customerId" does not match to a Customer.', Comment = 'customerId is a field name and should not be translated.';
CustomerNumberDoesNotMatchACustomerErr: Label 'The "customerNumber" does not match to a Customer.', Comment = 'customerNumber is a field name and should not be translated.';
AppliesToDocValuesDontMatchErr: Label 'The Applies To Invoice values do not match to the same Invoice.';
AppliesToInvoiceIdDoesNotMatchAnInvoiceErr: Label 'The "appliesToInvoiceId" should be the ID of an Open, Paid, Corrective, or Canceled Invoice.', Comment = 'appliesToInvoiceId is a field name and should not be translated.';
FiltersChecked: Boolean;
BlankGUID: Guid;
local procedure TransferGeneratedFieldsFromInitializeLine(var GenJournalLine: Record "Gen. Journal Line")
begin
if GenJournalLine."Document No." = '' then
GenJournalLine."Document No." := Rec."Document No.";
end;
local procedure SetCalculatedFields()
begin
AppliesToInvoiceNumberText := Rec."Applies-to Doc. No.";
AppliesToInvoiceIdText := Rec."Applies-to Invoice Id";
end;
local procedure ClearCalculatedFields()
begin
Clear(AppliesToInvoiceIdText);
Clear(AppliesToInvoiceNumberText);
end;
local procedure ProcessAppliesToInvoiceNumberAndId()
begin
if AppliesToInvoiceNumberText <> '' then
Rec."Applies-to Doc. No." := AppliesToInvoiceNumberText;
Rec."Applies-to Invoice Id" := AppliesToInvoiceIdText;
end;
local procedure CheckFilters()
begin
if (Rec.GetFilter("Journal Batch Id") = '') and
(Rec.GetFilter(SystemId) = '')
then
Error(FiltersNotSpecifiedErr);
end;
}